Basically I have
void FileReader::parseBuffer(char * buffer, int length)
{
//start by looking for a vrsn
//Header seek around for a vrns followed by 32 bit size descriptor
//read 32 bits at a time
int cursor = 0;
char vrsn[5] = "vrsn";
cursor = this->searchForMarker(cursor, length, vrsn, buffer);
int32_t size = this->getObjectSizeForMarker(cursor, length, buffer);
cursor = cursor + 8; //advance cursor past marker and size
wchar_t *version = this->getObjectForSizeAndCursor(size, cursor, buffer);
cout << version << "\n";
delete[] version;
}
wchar_t* FileReader::getObjectForSizeAndCursor(int32_t size, int cursor, char *buffer) {
wchar_t *destination = NULL;
destination = new wchar_t[(size/2)+1];
memcpy(destination, buffer + cursor, size);
return destination;
}
in my example say i have the following bytes
7672736E – marker vrsn
00000040 – size of string to follow
0032002E0030002F00530065007200610074006F002000530063007200610074006300680020004C004900560045002000440061007400610062006100730065 – string
the string uses 16 bytes per character, so i cannot use a char * for the actual string, wchar_t seems like the best bet.
However when i memcpy these bytes to a wchar_t i get 0x7fe7abc037e0 in cout which i assume is a pointer?
which seems wrong. when i use wcout i get nothing in the terminal.
Will memcpy not work for this?
also should my wchar_t size be halved since i only have half as many wchar_t’s as i would have chars?
size is a byte count.
Your string looks like it’s in big endian order, but you are (probably) using a little-endian machine. Therefore, your
wchar_tstring is full of characters like U+3200 which your terminal might not correctly print out.You will have to properly flip every one of those characters. Your best bet is to just treat the input as a
unsigned char *and copy the elements one-at-a-time into yourwchar_t *with the correct endian reversal:Note that you can’t assume
wchar_tis 16 bits wide. If you can use C++11 features, usechar16_t.