It is a common thing to pass binary data through text-based protocols. One way to manage this is by using Base64 coding.
Base64 is an encoding which uses characters from 'A'-'Z', 'a'-'z', '0'-'9', and the '+', '/' characters. The '=' character is used for padding data.
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
The last two characters may change on different systems. One of the reasons could be that you cannot use pure Base64 to send binary data embedded into a URL because the '+' and '/' characters must be encoded with '%2B' and '%2F'. This increases the size of the code, and there may be additional problems (the % character is used by SQL). So sometimes, '+' and '/' have to be replaced; one of the standard replacements are '*' and '-' characters.
Using the Code
Coding is implemented in the
Base64 class. It can be used in native and managed applications. The version is chosen by the conditional compilation based on the
/clr project options. Macros in the ManagedExt.h header are used to make a transfer from one environment to another easier.
AS_PUBLIC CLASS Base64
static CONST wchar_t CHAR_63 = '*';
static CONST wchar_t CHAR_64 = '-';
static int Encode(BYTE_DATA_IN inData, int dataLength, STRING_OUT outCode);
static int Decode(STRING_IN inCode, int codeLength, BYTE_DATA_OUT outData);
static int GetDataLength(int codeLength);
static int GetCodeLength(int dataLength);
Encode - Converts an array of binary data into a string which contains Base64 code. It also returns the size of the produced code (in bytes).
Decode - Decodes a string which contains Base64 code and stores it into an output binary data buffer. If the native C++ version is used, the buffer must be pre-allocated. It also returns the number of decoded bytes stored in the output buffer.
GetDataLength - Calculates the maximum size of data (in bytes) based on the Base64 code's length.
GetCodeLength - Calculates the size of the Base64 code (in bytes) based on the code length. It also includes padding (if it is required). That means that the
Decode method can decode less bytes (one or two) than this method returns for the same code length.
CHAR_64 - These attributes are used to customize the Base64 code to fit our needs (described at the beginning of the article).
The managed version uses the
System::Convert class (
ToBase64String). The .NET framework always produces Base64 code with the '+' and '/' characters. The methods of this class replace them with
CHAR_64, if they are different (when coding), and reverse the changes before decoding.
GetDataLength method is handy in the native version of the library because the output buffer should be pre-allocated, and this is an easy way to find out how much memory you need.
- 7 November - Fixed the decode routine (miscalculation of size of decoded data).
- 7 January - A few bug fixes. Customization of padding char added.
- 5 January - Original version posted.