C ++ builder - converts UnicodeString to UTF-8 encoded string

I am trying to convert a UnicodeString to a UTF-8 encoded string in a C ++ builder. For this, I use the UnicodeToUtf8 () function.

char * dest; UnicodeSring src; UnicodeToUtf8(dest,256,src.w_str(),src.Length()); 

but the compiler shows me a runtime violation message. What am I doing wrong?

+4
source share
2 answers

This fixes the issue in the question, but the real way to convert UTF16 to UTF8 is in Remy's answer below.

dest is a pointer to a random space in memory, because you are not initializing it. In debug builds, it probably points to 0, but in versions it can be anywhere. You tell UnicodeToUtf8 that dest is a 256-character buffer.

try it

 char dest[256]; // room for 256 characters UnicodeString src = L"Test this"; UnicodeToUtf8( dest, 256, src, src.Length() ); 

But actually you can use it easier:

 char dest[256]; // room for 256 characters UnicodeString src = L"Test this"; UnicodeToUtf8( dest, src, 256 ); 
+3
source

Assuming you are using C ++ Builder 2009 or later (you did not say), and using the RTL System::UnicodeString class (and not some other third-party UnicodeString class), then there is a much simpler way to handle this situation . C ++ Builder also has the System::UTF8String (it was available with C ++ Builder 6, but did not become a true RTL-implemented UTF-8 string type until C ++ Builder 2009). Just assign UnicodeString UTF8String and let RTL handle the memory allocation and data conversion for you, for example:

 UnicodeString src = ...; UTF8String dest = src; // <-- automatic UTF16-to-UTF8 conversion // use dest.c_str() and dest.Length() as needed... 
+8
source

All Articles