2
int main(){ //"Chào" in Vietnamese wchar_t utf16[] =L"\x00ff\x00fe\x0043\x0000\x0068\x0000\x00EO\x0000\x006F"; //Dump utf16: FF FE 43 0 68 0 E 4F 0 6F (right) int size = WideCharToMultiByte(CP_UTF8,0,utf16,-1,NULL,0,NULL,NULL); char *utf8 = new char[size]; int k = WideCharToMultiByte(CP_UTF8,0,utf16,-1,utf8 ,size,NULL,NULL); //Dump utf8: ffffffc3 fffffbf ffffc3 ffffbe 43 0 } 

Here is my code, when i convert it string into UTF-8, it show a wrong result, so what is wrong with my code?

6
  • For starters, you probably want to convert your whole array, even though it is not a wide character string: It has embedded zeroes. Commented Apr 11, 2014 at 14:59
  • Also, unicode is not a synonym of utf-16. Commented Apr 11, 2014 at 15:03
  • 1
    @Deduplicator unfortunately Microsoft thinks otherwise. Commented Apr 11, 2014 at 15:06
  • So can you suggest a solution for this ?, how to make a properly utf-16 string in C++ Commented Apr 11, 2014 at 15:07
  • @MarkRansom: No reason to add to the confusion. Also, the OP will surely stumble across UTF-32 soon, as he already did for UTF-8. Commented Apr 11, 2014 at 15:09

2 Answers 2

0
wchar_t utf16[] = L"\uFEFFChào"; int size = 5; for (int i = 0; i < size; ++i) { std::printf("%X ", utf16[i]); } 

This program prints out: FEFF 43 68 E0 6F

If printing out each wchar_t you've read from a file prints out FF FE 43 0 68 0 E 4F 0 6F then the UTF-16 data is not being read from the file correctly.. Those values represent the UTF-16 string: `L"ÿþC\0h\0à\0o".

You don't show your code for reading from the file, but here's one way to do it correctly:

https://stackoverflow.com/a/10504278/365496

Sign up to request clarification or add additional context in comments.

Comments

0

You're reading the file incorrectly. Your dump of the input is showing single bytes in wide characters. Your dump of the output is the byte sequence that results from encoding L"\xff\xfe\x43" to UTF-8. The string is being truncated at the first \x0000 in the input.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.