Encode your binary data using Base64 or bin.hex, which both produce a string. Then pass this string into the data of your element with either the SetData or AddElem method.

Make sure the producer and consumer of your XML know how to encode and decode it respectively. Here is an example of an element that contains binary data encoded in Base64 and uses an attribute to indicate the type of encoding:

<IMAGE encoding="base64">
LZPVtzlndhYFJQIDAQABMA0GCSqGSIb3DQEBAgUAA1kACKr0PqphJYw1j+YPtcIq
iWlFPuN5jJ79Khfg7ASFxskYkEMjRNZV/HZDZQEhtVaU7Jxfzs2wfX5byMp2X3U/
5XUXGx7qusDgHQGs7Jk9W8CW1fuSWUgN4w==
</IMAGE>

Base64 is only 33% larger than the unencoded binary data, whereas hex is 100% larger. Base64 usually has lines that are 64 characters long, with the last line a multiple of 4 characters. It uses a 64 character set consisting of upper case letters, lower case letters, numbers 0 to 9, plus, and slash. It also uses the equal sign for padding at the end, and CRLF characters to break it into lines.

The idea of Base64 is that you lay all of the binary bytes together as an array of bits and then take 6 bits at a time and produce a printable character. Since 6 bits is a number from 0 to 63, you use one of the 64 characters listed above for each 6 bit unit. It is fast because there is no compression algorithm or encryption. Every 3 bytes can be represented as 4 printable characters (3:4), hence the 33% increase in size.

Base64 is part of MIME; the specification is in RFC 1421. The CMarkup developer version has Base64 Functions EncodeBase64 and DecodeBase64.

 

comment posted CMarkup data question

Andy 01-May-2007

My question is how do I add data ( hex ) to an xml file. I have read about the "CData" Section but this still needs text. I need to store about 20Kbytes in different sections. Do I need a fast way to convert the data into text or can I add the raw data into a section of it's own and retrieve it?

You are right, CDATA Sections are for "unparsed text," but still text. You need to convert your raw data to text before you store it in the XML.