JUCE String and BOM


I have a need to take a string, put it into a memory block, do stuff to it(encrypt it) and then finally base64Encode it, and store it in an XML file. And then of course do the whole thing in reverse.

This works, but i’m having a problem with byte order when I put the string into the memoryblock. What i’d really like is to be able to get a MemoryBlock from a string with a BOM. This is something I could write myself no problem, but I thought it’d be anice complement to the existing createStringFromData function.

My problem is if I read that XML file on the same platform its fine, but if it was created on an IntelMac and read on a PowerPC then pulling a string out of the memory block doesn’t work(these are UTF-16 strings, using simple casts to get the string in and out of the memoryblock works on the same processor, but obviously doesn’t when they are not!)


What might be nice would be a toUnicode() function - UTF16. Casting (const juce_wchar*) on the mac gives me a 32 bit representation.

so the string “disk” internally on my intelmac is (in a juce::String)

64 00 00 00 69 00 00 00 73 00 00 00 6b 00 00 00

It’d be nice to get this as UTF-16 with a BOM

Then this would work with the current createStringFromData()


how about

const int len = sourceString.length();
const juce_wchar* t = (const juce_wchar*) sourceString;
UniChar* temp = (UniChar*) juce_malloc (sizeof (UniChar) * len + 1);
UniChar *dst=temp;
*dst++=0xFEFF; // BOM Marker
for (int i = 0; i <= len; ++i, dst++)
*dst = t[i];

			MemoryBlock memBlock(temp,(len+1)*sizeof(UniChar));
			juce_free (temp);

return memBlock;