views:

1171

answers:

4

Suppose I have this Windows wchar_t string:

L"\x4f60\x597d"

and

L"\x00e4\x00a0\x597d"

and would like to convert it (not neccessarily programatically; it will be a one-time thing) to GCC/Linux wchar_t format, which is UTF-32 AFAIK. How do I do it? (a general explanation would be nice, but example based on this concrete case would be helpful as well)

Please don't direct me to character conversion sites. I would like to convert from L"\x(something)" form and not "end character" form.

+1  A: 

Would converting from UTF-16 (the Visual C++ wchar_t form) to UTF-8, then possibly from UTF-8 to UCS-4 (the GCC wchar_t form), be an acceptable answer?

If so, then in Windows you could use the WideCharToMultiByte function (with CP_UTF8 for the CodePage parameter), for the first part of the conversion. Then you could either paste the resulting UTF-8 strings directly into your program, or convert them further. Here is a message showing how one person did it; you can also write your own code or do it manually (the official spec, with a section on exactly how to convert UTF-8 to UCS-4, can be found here). There may be an easier way, I'm not overly familiar with the conversion stuff in Linux yet.

Head Geek
ah, you beat me to the answer. +1
aib
+2  A: 

You only need to worry about characters between \xD800 and \xDFFF inclusive. Every other character should map exactly the same from UTF-16 to UCS-4 when zero-filled.

Ignacio Vazquez-Abrams
A: 

One of the most used libraries to do character conversion is the ICU library http://icu-project.org/ It is e.g. used by some boost http://www.boost.org/ libraries.

lothar
A: 

Ignacio is right, if you don't use some rare Chinese characters (or some extinct scripts), then the mapping is one to one. (the official "lingo" is "if you don't have characters outside BMP")

This is the algorithm, just in case: http://unicode.org/faq/utf_bom.html#utf16-3 But again, most likely useless for your real case.

You can also use the free sources from Unicode (ftp://ftp.unicode.org/Public/PROGRAMS/CVTUTF)

Mihai Nita