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.

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_63 and 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 (FromBase64String and ToBase64String). The .NET framework always produces Base64 code with the '+' and '/' characters. The methods of this class replace them with CHAR_63 and CHAR_64, if they are different (when coding), and reverse the changes before decoding.

The 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.

History

7 November - Fixed the decode routine (miscalculation of size of decoded data).

If you're using another decode (not the one provided in this project) method you should try modifying CHAR_63, CHAR_64 and CHAR_PAD to +/= (this project uses modified set: *-[ so that encoded data can be sent as part of a URL, I think the article explains it).

BTW, sorry for the late response, yet again I didn't get e-mail notification of your post.

Thanks. Uh, just remembered that I found this bug myself earlier but forgot to send update. Now I can't remember complete explanation why this fix works (it was months ago), but anyway. To solve the problem, you should insert else branch here: