String problem

I’m converting some code from an older version of juce, where I could do a copyFromBuffer() to extract a char from a String.

My replacement code looks like this:

String	theTestString = ";";
CharPointer_UTF8::CharType	theUTF8String;
int	theStringLength = theTestString.getNumBytesAsUTF8();
theTestString.copyToUTF8(&theUTF8String, theStringLength);

Sadly, theUTF8String is empty after doing this.

Maybe someone out there can point out the error of my ways…

It looks as if the correct character is extracted by copyWithDestByteLimit(), but then, because the maxBytes value was decremented both before and after the read, the routine exits without assigning the read value to the return variable.

Agh! You’re not actually allocating a buffer to copy it into - you’re just writing the data all over your stack! CharPointer_UTF8::CharType isn’t some kind of magical object that will allocate a buffer for you, it’s just a typedef for ‘char’ !