Base 64 Encoding

Base 64 enconding is a quite common way of transmitting binary data this days. It’s based on 26 characters shown in this table:

base64

For each character encoding we need indeed 6 bits. One more character is used for padding, the =”

Let’s see then how would you encode the message “Man”, which does not need padding:

Selection_012

That is Man is encoded as TWFu.
The pseudo-code for padding is:

if (( text.length % 3) != 0) then {
Right padding with zeros encoded with ‘=’.
Number of added bytes is equal number of ‘=’ .
}

For instance, Face in base 64 is RmFjZQ==:

Face

You can see the extra zeros bits highlighted with yellow.
Applying the pseudo-code to determine the number of extra bytes needed:

8 * 4 = 32 bits length of text.
( 32 + ( 8 * 0 )) % 3 = 10,67 //NOK
( 32 + ( 8 * 1 )) % 3 = 13,3(3) //NOK
( 32 + ( 8 * 2 )) % 3 = 0 // OK!

And we’ve indeed used 2 = characters.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s