# Base 64 encoding

Discussion in 'iOS Programming' started by Nnavick, Mar 29, 2011.

1. ### Nnavick macrumors regular

Joined:
Oct 13, 2010
#1
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5)

I want to write an algoritm that does bade64 encoding.

I saw in Wikipedia a chart but I don't understand the last part :
Text context M ASCII is 77 the binary part I understood but I dont understand the index part ,how they got 19??

Text content M a n
ASCII 77 97 110
Bit pattern 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 1 0 1 1 0 1 1 1 0
Index 19 22 5 46
Base64-encoded T W F u

http://en.m.wikipedia.org/wiki/Base64
Thanks!!!

2. ### sammich macrumors 601

Joined:
Sep 26, 2006
Location:
Sarcasmville.
3. ### KnightWRX macrumors Pentium

Joined:
Jan 28, 2009
Location:
#3
Base 64 encoding is basically taking 8 bit characters and making them 6 bit.

Considering binary positions indicate a certain power of 2 value, you can simply add up the values of the 1s (128 64 32 16 8 4 2 1) and lookup the ASCII value to get the indices :

0 1 0 0 1 1 0 1 - (64 + 8 + 4 + 1) - 77 - M
0 1 1 0 0 0 0 1 - (64 + 32 + 1) - 97 - a
0 1 1 0 1 1 1 0 - ( 64 + 32 + 8 + 4 + 2) - 110 - n

Now, since we're taking these and converting them to 6 bit (with 2 leading 00s), we need to split this up evenly to not lose any information (we can't just bit shift by 2, we'll lose the 2 1 and positions). So we take the whole 24 bit chain, (3 x 8 bit characters) and split them up into 4 6 bit chains.

0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 1 0 1 1 0 1 1 1 0

Add 2 leading 0s to each and you now get how base64 encoded string to which you can apply our same math as earlier :

0 0 0 1 0 0 1 1 - (16 + 2 + 1) - 19
0 0 0 1 0 1 1 0 - (16 + 4 + 2) - 22
0 0 0 0 0 1 0 1 - (4 + 1) - 5
0 0 1 0 1 1 1 0 - (32 + 8 + 4 + 2) - 46

This is the tricky part. These aren't ASCII characters. You can't simply store this number in a char and then printf("%c"); the output and hope to get a valid Base64 string. These are "index" values to the Base64 Alphabet. It is defined in RFC 3548 :

Code:
```                Table 1: The Base 64 Alphabet

Value Encoding  Value Encoding  Value Encoding  Value Encoding
0 A            17 R            34 i            51 z
1 B            18 S            35 j            52 0
2 C            19 T            36 k            53 1
3 D            20 U            37 l            54 2
4 E            21 V            38 m            55 3
5 F            22 W            39 n            56 4
6 G            23 X            40 o            57 5
7 H            24 Y            41 p            58 6
8 I            25 Z            42 q            59 7
9 J            26 a            43 r            60 8
10 K            27 b            44 s            61 9
11 L            28 c            45 t            62 +
12 M            29 d            46 u            63 /
13 N            30 e            47 v
14 O            31 f            48 w         (pad) =
15 P            32 g            49 x
16 Q            33 h            50 y
```
That's where TFWu (19 22 5 46) comes from.