ui/shared/src/main/java/com/squareup/subzero/shared/Base45.java
public static String toBase45(byte[] input) {
    // input expands by approximately a factor of 1.5 (1.5 comes from log(256) / log(45))
    StringBuilder output = new StringBuilder((int)(input.length * 1.5) + 3);
    int acc = 0;
    int counter = 0;

    // consume the input buffer
    for (int i = 0; i < input.length; i++) {
      // convert signed byte to unsigned int
      int inputByte = (input[i] & 0xff);
      // read 2 bytes at a time
      acc = (acc << 8) | inputByte;
      counter++;

      if (counter == 2) {
        // emit acc and reset
        int s0 = (acc / 45) / 45;
        int s1 = (acc / 45) % 45;
        int s2 = (acc % 45);
        if (s0 >= 45) {
          throw new IllegalStateException(format("s0 should be <45, but is %d", s0));
        }
        output.append(CHARSET.charAt(s0));
        output.append(CHARSET.charAt(s1));
        output.append(CHARSET.charAt(s2));

        acc = 0;
        counter = 0;
      }
    }

    if (counter > 0) {
      // if we blindly emit the last value, we won't be able to tell even from odd length inputs
      // apart. So we emit 2 chars for odd length inputs.
      int s0 = acc / 45;
      int s1 = acc % 45;
      if (s0 >= 45) {
        throw new IllegalStateException(format("s0 should be <45, but is %d", s0));
      }
      output.append(CHARSET.charAt(s0));
      output.append(CHARSET.charAt(s1));
    }
    return output.toString();
  }
/**
   * Encodes a byte array to a Base45-encoded String.
   */
Encodes a byte array to a Base45-encoded String.
Encodes a byte array to a Base45 - encoded String .
public static String toBase45(byte[] input) {
    StringBuilder output = new StringBuilder((int)(input.length * 1.5) + 3);
    int acc = 0;
    int counter = 0;
    for (int i = 0; i < input.length; i++) {
      int inputByte = (input[i] & 0xff);
      acc = (acc << 8) | inputByte;
      counter++;
      if (counter == 2) {
        int s0 = (acc / 45) / 45;
        int s1 = (acc / 45) % 45;
        int s2 = (acc % 45);
        if (s0 >= 45) {
          throw new IllegalStateException(format("s0 should be <45, but is %d", s0));
        }
        output.append(CHARSET.charAt(s0));
        output.append(CHARSET.charAt(s1));
        output.append(CHARSET.charAt(s2));
        acc = 0;
        counter = 0;
      }
    }
    if (counter > 0) {
      int s0 = acc / 45;
      int s1 = acc % 45;
      if (s0 >= 45) {
        throw new IllegalStateException(format("s0 should be <45, but is %d", s0));
      }
      output.append(CHARSET.charAt(s0));
      output.append(CHARSET.charAt(s1));
    }
    return output.toString();
  }
public static String toBase45 ( byte [ ] input ) { StringBuilder output = new StringBuilder ( ( int ) ( input . length * 1.5 ) + 3 ) ; int acc = 0 ; int counter = 0 ; for ( int i = 0 ; i < input . length ; i ++ ) { int inputByte = ( input [ i ] & 0xff ) ; acc = ( acc << 8 ) | inputByte ; counter ++ ; if ( counter == 2 ) { int s0 = ( acc / 45 ) / 45 ; int s1 = ( acc / 45 ) % 45 ; int s2 = ( acc % 45 ) ; if ( s0 >= 45 ) { throw new IllegalStateException ( format ( "s0 should be <45, but is %d" , s0 ) ) ; } output . append ( CHARSET . charAt ( s0 ) ) ; output . append ( CHARSET . charAt ( s1 ) ) ; output . append ( CHARSET . charAt ( s2 ) ) ; acc = 0 ; counter = 0 ; } } if ( counter > 0 ) { int s0 = acc / 45 ; int s1 = acc % 45 ; if ( s0 >= 45 ) { throw new IllegalStateException ( format ( "s0 should be <45, but is %d" , s0 ) ) ; } output . append ( CHARSET . charAt ( s0 ) ) ; output . append ( CHARSET . charAt ( s1 ) ) ; } return output . toString ( ) ; }
Encodes a byte array to a Base45-encoded String.
Encodes a byte array to a Base45 - encoded String .
// input expands by approximately a factor of 1.5 (1.5 comes from log(256) / log(45)) // consume the input buffer // convert signed byte to unsigned int // read 2 bytes at a time // emit acc and reset // if we blindly emit the last value, we won't be able to tell even from odd length inputs // apart. So we emit 2 chars for odd length inputs.