|
Boost Users : |
Subject: Re: [Boost-users] [serialization?] converting utf8 string to unicode wstring
From: OvermindDL1 (overminddl1_at_[hidden])
Date: 2009-10-07 17:23:49
On Wed, Oct 7, 2009 at 11:58 AM, Igor R <boost.lists_at_[hidden]> wrote:
> Hello,
>
> I try to accomplish the subj with help of boost's utf8_codecvt_facet.
> I based my code on this example:
> http://www.boost.org/doc/libs/1_40_0/libs/serialization/doc/codecvt.html%c2 .
> The only difference is that my utf8 text resides in std::string:
>
> #include <sstream>
> #include <iostream>
> #include "boost/archive/detail/utf8_codecvt_facet.hpp"
> // link with boost/libs/serialization/src/utf8_codecvt_facet.cpp
>
> int main()
> {
> Â std::string utf;
> Â utf.resize(11);
> Â //Â hardcode some utf8 text
> Â utf[0] = 0xd7;
> Â utf[1] = 0x90;
> Â utf[2] = 0xd7;
> Â utf[3] = 0x99;
> Â utf[4] = 0xd7;
> Â utf[5] = 0x92;
> Â utf[6] = 0xd7;
> Â utf[7] = 0x95;
> Â utf[8] = 0xd7;
> Â utf[9] = 0xa8;
> Â utf[10] = 0x0;
> Â std::locale old_locale;
> Â std::locale utf8_locale(old_locale, new
> boost::archive::detail::utf8_codecvt_facet());
> Â std::locale::global(utf8_locale);
> Â std::stringstream in;
> Â in.imbue(utf8_locale);
> Â in.str(utf);
> Â std::wstringstream out;
> Â out << in;
> Â std::wcout << out.str() << std::endl;
> }
>
> The above code doesn't work: "out"Â buffer doesn't contain correct unicode
> interpretation of the string.
> Actually, all i want is a c++ equivalent to the following WinAPI:
>
> #include "windows.h"
> int main()
> {
> Â std::string utf;
> Â utf.resize(11);
> Â //Â hardcode some utf8 text
> Â utf[0] = 0xd7;
> Â utf[1] = 0x90;
> Â utf[2] = 0xd7;
> Â utf[3] = 0x99;
> Â utf[4] = 0xd7;
> Â utf[5] = 0x92;
> Â utf[6] = 0xd7;
> Â utf[7] = 0x95;
> Â utf[8] = 0xd7;
> Â utf[9] = 0xa8;
> Â utf[10] = 0x0;
> Â wchar_t outBuff[11];
> Â MultiByteToWideChar(CP_UTF8, 0, utf.c_str(), -1, outBuff, 10);
> }
>
> ...which works well.
>
> Any idea would be greatly appreciated!
Er... I thought UTF8 *is* a form of Unicode?
Looks like you are trying to convert UTF8 to UTF16, for what reason?
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net