public static String encode ( final byte [ ] bytes ) { StringBuilder base32 = new StringBuilder ( ( bytes . length * 8 + 4 ) / 5 ) ; int currByte , digit , i = 0 ; while ( i < bytes . length ) { // STEP 0; insert new 5 bits, leave 3 bits currByte = bytes [ i ++ ] & 255 ; base32 . append ( CHARS [ currByte >> 3 ] ) ; digit = ( currByte & 7 ) << 2 ; if ( i >= bytes . length ) { base32 . append ( CHARS [ digit ] ) ; break ; } // STEP 3: insert 2 new bits, then 5 bits, leave 1 bit currByte = bytes [ i ++ ] & 255 ; base32 . append ( CHARS [ digit | ( currByte >> 6 ) ] ) ; base32 . append ( CHARS [ ( currByte >> 1 ) & 31 ] ) ; digit = ( currByte & 1 ) << 4 ; if ( i >= bytes . length ) { base32 . append ( CHARS [ digit ] ) ; break ; } // STEP 1: insert 4 new bits, leave 4 bit currByte = bytes [ i ++ ] & 255 ; base32 . append ( CHARS [ digit | ( currByte >> 4 ) ] ) ; digit = ( currByte & 15 ) << 1 ; if ( i >= bytes . length ) { base32 . append ( CHARS [ digit ] ) ; break ; } // STEP 4: insert 1 new bit, then 5 bits, leave 2 bits currByte = bytes [ i ++ ] & 255 ; base32 . append ( CHARS [ digit | ( currByte >> 7 ) ] ) ; base32 . append ( CHARS [ ( currByte >> 2 ) & 31 ] ) ; digit = ( currByte & 3 ) << 3 ; if ( i >= bytes . length ) { base32 . append ( CHARS [ digit ] ) ; break ; } // STEP 2: insert 3 new bits, then 5 bits, leave 0 bit currByte = bytes [ i ++ ] & 255 ; base32 . append ( CHARS [ digit | ( currByte >> 5 ) ] ) ; base32 . append ( CHARS [ currByte & 31 ] ) ; } return base32 . toString ( ) ; }
Encode an array of binary bytes into a Base32 string .