HOWTO Use Encoding
The Encoding class in POJava is for the type of encoding used to represent binary data in a more
portable format. It currently includes Base64 and Hexadecimal encoding and decoding. Hexadecimal
is familiar even to new programmers, while Base64 is only a little less so. Both encoding methods
use a fixed set of characters to represent a sequence of bits. The main difference between the two is
that Hexadecimal characters each represent 4 bits, and Base64 characters each represent 6 bits.
Encoding and decoding data in either format using POJava is simple. Here is an example.
// Encoding
String str="This is the data I wish to encode.";
char[] encoded=EncodingTool.base64Encode(str.getBytes());
// Decoding
String decoded=EncodingTool.base64Decode(encoded);
assertEquals(str, decoded);
If your encoded Base64 content contains spaces and carriage returns, you can pass it as a String
to the base64Decode method and it will ignore the whitespace in the String. Passing in a byte
array will prompt a more strict interpretation of allowed characters.
String encodedString=new String(encoded);
String decoded=EncodingTool.base64Decode(encoded);
Reversibility
Both Base64 and Hexadecimal encodings are reversible without any loss of data. Both represent an
exact sequence of ones and zeros.
Portability
The notion of portability implies that there are places where pure binary data can't go without
some sort of conversion. Where might this happen? I've seen Hexadecimal encoding used most frequently
on a computer screen. Some byte values of a character set represent unviewable values like null,
backspace, clear or down, so displaying a memory or packet dump of raw data often uses hexadecimal
instead. Base64 is frequently associated with printable documents, like MIME-encoded e-mails or
with binary data stored in an XML or HTML document.
Hexadecimal Encoding
Hexadecimal encoding takes a series of bytes, and represents each sequence of 4 bits with
an individual character. The ASCII characters '0' through '9' map to the first ten bit combinations,
and the characters 'A' - 'F' represent the last six, adding up to all 16 possible combinations.
| Bits | Dec | Hex |
| 0000 | 0 | 0 |
| 0001 | 1 | 1 |
| 0010 | 2 | 2 |
| 0011 | 3 | 3 |
| 0100 | 4 | 4 |
| 0101 | 5 | 5 |
| 0110 | 6 | 6 |
| 0111 | 7 | 7 |
|
| Bits | Dec | Hex |
| 1000 | 8 | 8 |
| 1001 | 9 | 9 |
| 1010 | 10 | A |
| 1011 | 11 | B |
| 1100 | 12 | C |
| 1101 | 13 | D |
| 1110 | 14 | E |
| 1111 | 15 | F |
|
Base64 Encoding
Base64 converts a sequence of bytes into a sequence of characters, each representing
six bits. Each sequence of three unencoded bytes will fall neatly into a sequence of four
encoded characters, each representing the same twenty-four bits.
If the sequence of bytes to encode doesn't fall evenly into three bytes at a time, the remaining bytes
are still sequenced into four encoded characters. Equals signs are used to represent the filler portion
that will not be decoded into the original data. Depending on the length of the original data, the
encoded characters may end in zero, one or two equals signs.
| Bits | Dec | Base64 |
| 000000 | 0 | A |
| 000001 | 1 | B |
| 000010 | 2 | C |
| 000011 | 3 | D |
| 000100 | 4 | E |
| 000101 | 5 | F |
| 000110 | 6 | G |
| 000111 | 7 | H |
| 001000 | 8 | I |
| 001001 | 9 | J |
| 001010 | 10 | K |
| 001011 | 11 | L |
| 001100 | 12 | M |
| 001101 | 13 | N |
| 001110 | 14 | O |
| 001111 | 15 | P |
|
| Bits | Dec | Base64 |
| 010000 | 16 | Q |
| 010001 | 17 | R |
| 010010 | 18 | S |
| 010011 | 19 | T |
| 010100 | 20 | U |
| 010101 | 21 | V |
| 010110 | 22 | W |
| 010111 | 23 | X |
| 011000 | 24 | Y |
| 011001 | 25 | Z |
| 011010 | 26 | a |
| 011011 | 27 | b |
| 011100 | 28 | c |
| 011101 | 29 | d |
| 011110 | 30 | e |
| 011111 | 31 | f |
|
| Bits | Dec | Base64 |
| 100000 | 32 | g |
| 100001 | 33 | h |
| 100010 | 34 | i |
| 100011 | 35 | j |
| 100100 | 36 | k |
| 100101 | 37 | l |
| 100110 | 38 | m |
| 100111 | 39 | n |
| 101000 | 40 | o |
| 101001 | 41 | p |
| 101010 | 42 | q |
| 101011 | 43 | r |
| 101100 | 44 | s |
| 101101 | 45 | t |
| 101110 | 46 | u |
| 101111 | 47 | v |
|
| Bits | Dec | Base64 |
| 110000 | 48 | w |
| 110001 | 49 | x |
| 110010 | 50 | y |
| 110011 | 51 | z |
| 110100 | 52 | 0 |
| 110101 | 53 | 1 |
| 110110 | 54 | 2 |
| 110111 | 55 | 3 |
| 111000 | 56 | 4 |
| 111001 | 57 | 5 |
| 111010 | 58 | 6 |
| 111011 | 59 | 7 |
| 111100 | 60 | 8 |
| 111101 | 61 | 9 |
| 111110 | 62 | + |
| 111111 | 63 | / |
|
Encoding is not Encryption
Even though a person cannot easily interpret Base64 encoded content without the aid of a computer,
one should not confuse it with encryption. Base64 offers no privacy. It is not weak encryption--
it is simply a representation of data in a more portable format.