Jimm
Jimm

Reputation: 8505

Storing C++ binary output in xml

As per xsd, the supported binary types are hexbinary and base64 encoded binary data. http://www.w3schools.com/schema/schema_dtypes_misc.asp

My intention is to read raw byte contents from the memory and serialize it to the xml file. Hence, what data type above would describe the raw byte contents OR do i have to make sure that the raw byte contents are converted to hexdecimal to adhere to one of the 2 data types described above ?

Upvotes: 0

Views: 2311

Answers (2)

Mark Ransom
Mark Ransom

Reputation: 308392

You need to encode the raw data to one of the two data types. This is to keep some random data from messing up the XML format, for example if you had a < embedded in the data somewhere.

You can choose whichever of the two is most convenient for you. The hexadecimal type is easier to write code for but produces a larger file - the ratio of bytes out to bytes in is 2:1, where it is 4:3 for the Base64 encoding. You shouldn't need to write your own code though, Base64 conversion functions are readily available. Here's a question that has some code in the answers: How do I base64 encode (decode) in C?

As an example of how the codings differ, here's the phrase "The quick brown fox jumps over the lazy dog." encoded both ways.

Hex:

54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f672e

Base64:

VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZy4=

Upvotes: 1

wholerabbit
wholerabbit

Reputation: 11566

You do have to convert the raw binary to hexadecimal (or base64) representation. Eg, if the value of the byte is 255 (in decimal), it's hex representation (as a string) would be "ff".

The (conventional) type to use for storing the raw input is unsigned char, so you can get the ranges 0-255 easily byte by byte, but for each byte of that array, you need two bytes in a signed char (or std::string) type to store the representation, and that is what you use in the XML.

Your framework probably has a method for converting raw bytes to Base64 or hex. If not, here's one method for hex:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main (void) {
    ostringstream os;
    os.flags(ios::hex);

    unsigned char data[] = { 0, 123, 11, 255, 66, 99 };

    for (int i = 0; i < 6; i++) {
        if (data[i] < 16) os << '0';
        os << (int)data[i] << '|';
    }

    string formatted(os.str());

    cout << formatted << endl;

    return 0;
}          

Outputs: 00|7b|0b|ff|42|63|

Upvotes: 3

Related Questions