I'm trying to do a very simple task: take a unicode-aware wstring
and convert it to a string
, encoded as UTF8 bytes, and then the opposite way around: take a string
containing UTF8 bytes and convert it to unicode-aware wstring
.
The problem is, I need it cross-platform and I need it work with Boost... and I just can't seem to figure a way to make it work. I've been toying with
Trying to convert the code to use stringstream
/wstringstream
instead of files of whatever, but nothing seems to work.
For instance, in Python it would look like so:
>>> u"????"
u'u05e9u05dcu05d5u05dd'
>>> u"????".encode("utf8")
'xd7xa9xd7x9cxd7x95xd7x9d'
>>> 'xd7xa9xd7x9cxd7x95xd7x9d'.decode("utf8")
u'u05e9u05dcu05d5u05dd'
What I'm ultimately after is this:
wchar_t uchars[] = {0x5e9, 0x5dc, 0x5d5, 0x5dd, 0};
wstring ws(uchars);
string s = encode_utf8(ws);
// s now holds "xd7xa9xd7x9cxd7x95xd7x9d"
wstring ws2 = decode_utf8(s);
// ws2 now holds {0x5e9, 0x5dc, 0x5d5, 0x5dd}
I really don't want to add another dependency on the ICU or something in that spirit... but to my understanding, it should be possible with Boost.
Some sample code would greatly be appreciated! Thanks
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…