wchar_t -> UTF-8?

J

Jon Willeke

This feels like a FAQ, but I've been unable to find a satisfactory
answer. Given a Unicode encoding (such as UCS-4) in wchar_t, I want to
convert to UTF-8 (or another locale-specific encoding) in a manner not
entirely unlike the following:

wstring w = L"H\xe9llo";

locale loc( "en_US.UTF-8" );
wcout.imbue( loc );
wcout << w << endl;

I've tried Visual C++ 6.0 and Borland C++ 5.6.4 on Windows, as well as
GCC 3.3.1 on Linux. They don't seem to do anything approximately like
this. I tried plugging in a hand-written codecvt subclass, but it
doesn't seem to be used.
 
J

John Ericson

Jon Willeke said:
This feels like a FAQ, but I've been unable to find a satisfactory
answer. Given a Unicode encoding (such as UCS-4) in wchar_t, I want to
convert to UTF-8 (or another locale-specific encoding) in a manner not
entirely unlike the following:

wstring w = L"H\xe9llo";

locale loc( "en_US.UTF-8" );
wcout.imbue( loc );
wcout << w << endl;

I've tried Visual C++ 6.0 and Borland C++ 5.6.4 on Windows, as well as
GCC 3.3.1 on Linux. They don't seem to do anything approximately like
this. I tried plugging in a hand-written codecvt subclass, but it
doesn't seem to be used.

IIRC, Dinkumware has a library for various code conversions.
You might want to Google a bit in comp.lang.c++ and
comp.lang.c++.moderated, since there are some good threads
in there on the various issues. Be prepared for some quirks,
depending on your system. Best regards, JE
 
T

Tilman Kuepper

Hello Jon,
[...] I tried plugging in a hand-written codecvt
subclass, but it doesn't seem to be used.

You can find some codecvt-facets as part of the
arabica xml parser toolkit:
http://www.jezuk.co.uk/cgi-bin/view/arabica

Boost has similar facets ready for download:
http://groups.yahoo.com/group/boost/files/utf/

Plauger wrote a pair of columns in the April and
May 1999 editions of the C/C++ Users Journal. You
can find the source code on the cuj server:
http://www.cuj.com/code/

Tilman
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top