HISPACTA Hell

Friday, June 26, 2009

Reminding myself about Base64

I've learned and forgotten Base64 a couple times now, and after I re-looked up how it worked, I thought I'd take advantage of the old adage about "you remember 80% of what you write" by blogging my own explanation of Base64.

Here goes...

Let's say you wanted to send the string "{|}" in an email as some sort of crude emoticon. Those are 3 characters that are in the ASCII character set, but they aren't in the Base64 encoding table. They could just as easily be 3 non-printable characters or maybe multi-byte UTF-16 character points, but let's just stick with {|} for this example.

The 3 bytes for those ASCII characters are:
{ = 7B = 123 = 01111011
| = 7C = 124 = 01111100
} = 7D = 135 = 01111101

String those 3 bytes together into a single 24 bit stream is:
011110110111110001111101

The number 24 is both (3 * 8) and (4 * 6), so splitting it into 4 x 6-bit chunks yeilds:
011110 = 30
110111 = 55
110001 = 49
111101 = 61

Now looking those up in the Base64 conversion chart yields:
011110 = 30 = e
110111 = 55 = 3
110001 = 49 = x
111101 = 61 = 9

So "{|}" is "e3x9" in Base64 encoding.

I found this nifty base64 encoder/decoder site to validate my gorilla math.

0 Comments:

Post a Comment

<< Home