You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
That is the Base64 character set. As you may expect, it's 64 characters long.
80
+
81
+
Let's say we want to somehow represent these two bytes in those 64 characters:
82
+
```text
83
+
00001001 10010010 # 09 92 in hex
84
+
```
85
+
To do that, Base64 does something very smart: it uses the bits, interpreted as a number, as indexes of the character set.
86
+
87
+
To explain what that means, let's consider what possible values 6 bits can represent: `000000` (decimal 0) to `111111` (decimal 63).
88
+
Since 0 to 63 is the exact range of indices that can be used with the 64 element character set, we'll group our 8 bit chunks (bytes) of data in 6 bit chunks (to use as indices):
89
+
```text
90
+
000010 011001 0010
91
+
```
92
+
`000010` is 2 in decimal, so by using it as an index of the character set, Base64 adds `C` (index 2) to the result.
93
+
94
+
`011001` is 16 + 8 + 1 = 25 in decimal, so Base64 appends `Z` (index 25) to the result.
95
+
96
+
You may have spotted a problem with the next chunk - it's only 4 bits long!
97
+
98
+
To get around this, Base64 pretends it's a 6 bit chunk and simply appends how many zeros are needed:
99
+
```
100
+
0010 + 00 => 001000
101
+
```
102
+
`001000` is 8 in decimal, so Base64 appends `I` to the result
103
+
104
+
But then, on the decoding side, how do you know where real data ends and where the pretend data starts?
105
+
106
+
It turns out that we don't need to do anything. On the decoding side, we know that the decoded data _has_ to be a multiple of 8 bits. So, the decoder ignores the bits which make the output _not_ a multiple of 8 bits, which will always be the extra bits we added.
107
+
108
+
Finally, encoding `00001001 10010010` to Base64 should result in `CZI`
109
+
110
+
Try this in your terminal with the real Base64!
111
+
```shell
112
+
echo -n -e \\x09\\x92 | base64 # base64 also adds a "=" character called "padding" to fit to a standard input length to output length ratio
113
+
```
114
+
115
+
### Aces
116
+
117
+
Now we generalize this to all character sets.
118
+
119
+
Generalizing the character set is easy, we just switch out the characters of the array storing the character set.
120
+
121
+
Changing the length of the character set is slightly harder. For every character set length, we need to figure out how many bits the chunked data should have.
122
+
123
+
In the Base64 example, the chunk length (let's call it that) was 6. The character set length was 64.
124
+
125
+
[comment]: <>(Let's do another example: in octal, the character set length is 8 and the chunk length will be 3 (`000` to `111` = 0 to 7))
126
+
127
+
[comment]: <>(For a character set length of 4, we'd need a chunk length of 2 (`00` to `11` is 0 to 3))
128
+
129
+
[comment]: <>(```text)
130
+
131
+
[comment]: <>(set len => chunk len)
132
+
133
+
[comment]: <>( 4 => 2)
134
+
135
+
[comment]: <>( 8 => 3)
136
+
137
+
[comment]: <>( 64 => 6)
138
+
139
+
[comment]: <>(```)
140
+
It looks like `2^(chunk len) = set len`. We can prove this is true with this observation:
141
+
142
+
Every bit can either be 1 or 0, so the total possible values of a certain number of bits will just be `2^(number of bits)` (if you need further proof, observe that every bit we add doubles the total possibilities since there's an additional choice: the new bit being 0 or the new bit being 1)
143
+
144
+
The total possible values is the length of the character set (of course, since we need the indices to cover all the characters of the set)
145
+
146
+
So, to find the number of bits the chunked data should have, we just do `log2(character set length)`. Then, we divide the bytes into chunks of that many bits (which was pretty hard to implement: knowing when to read more bytes, crossing over into the next byte to fetch more bits, etc, etc.), use those bits as indices for the user-supplied character set, and print the result. Easy! (Nope, this is the work of several showers and a lot of late night pondering :)
0 commit comments