The
base 64 Content-Transfer-Encoding is commonly used to attach images and other binary files to electronic mail. The encoding only uses characters which are unlikely to cause trouble across a wide range of different systems, since transmitting international characters and especially control codes is a recipy for catastrophe. The downside, on the other hand is that files become 33% longer.
The input data is concatenated in groups of 3 bytes, which are transformed to 4 six-bit characters, as per the following table: Padding may be needed as the input must consist of 24 bit groups.
Character range | Character codes |
A-Z | 0-25 |
a-z | 26-51 |
0-9 | 52-61 |
+ | 62 |
/ | 63 |
= | padding |
The line width must not exceed 76 characters, and whitespace, linefeed and carriage return characters must be ignored. Extraneous characters can be processed leniently (bypassed). Alternatively, an error can be reported to the user, especially if a premature equal sign is met. The program presented takes the lenient approach, of course.
Short
programs and x86 binary files are provided for this section. The routine which shifts a number of bits into a register and a different number out of it is interesting in its own right: It is useful in other instances, too, such as
Huffman coding.
For my own code up to this point: Valid XHTML 1.0!