Tutorial :Byte array to UTF8 CString



Question:

I'm using Visual Studio 2008 (C++). How do I create a CString (in a non-Unicode app) from a byte array that has a string encoded in UTF8 in it?

Thanks,

kreb

EDIT: Clarification: I guess what I'm asking is.. CStringA doesn't seem to be able to interpret a UTF8 string as UTF8, but rather as ASCII or the current codepage (I think).. How do I convert this UTF8 string to a CStringW? (UTF-16..?) Thanks


Solution:1

CStringW filename= CA2W(null_terminated_byte_buffer, CP_UTF8) should do the trick.


Solution:2

The nice thing about UTF8 is that every UTF8 string is also a valid NUL-terminated C string. That means that you should be able to simply cast a pointer to the first character of the byte array as a (const char *) and pass it to CString like you would any NUL-terminated C string.

Note that unless CString is aware of UTF8 semantics (I'm not familiar enough with CString to know exactly how it works, but I suspect isn't), certain operations that make sense on an ASCII C string may give strange results for a UTF8 C string. For example, a Reverse() method that reversed the order of the bytes in the string would not do the right thing for a UTF8 string, because it would not know to keep multi-byte characters together in the original order, and would reverse the bytes of the multi-byte character.


Solution:3

For most things, you can treat UTF8 the same as ASCII.

unsigned char szUtf8String[nSize] = "whatever";  CString s = static_cast<char *>(szUtf8String);  

That works for manipulating and writing to a file. However you cannot easily display the string, it will treat it as ASCII and misinterpret any non-english characters.

To display it, you will need to convert to UTF16 and possibly then back to ANSI (in the local code page).


Solution:4

Following the "MSN" answer above, I think that you will ultimately want a CString, not a CStringW out of it. So add a conversion back to CString:

CStringW filenameW = CA2W(null_terminated_byte_buffer, CP_UTF8); CString filename = CW2T( filenameW );


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »