Vla*_*rov 69 c++ unicode stl utf-8 character-encoding
是否有可能以独立于平台的方式将std :: string中的UTF8字符串转换为std :: wstring,反之亦然?在Windows应用程序中,我将使用MultiByteToWideChar和WideCharToMultiByte.但是,代码是针对多个操作系统编译的,我仅限于标准C++库.
Vla*_*rov 43
我5年前问过这个问题.这个帖子对我来说非常有帮助,我得出结论,然后我继续我的项目.有趣的是,我最近需要类似的东西,与过去的项目完全无关.在我研究可能的解决方案时,我偶然发现了自己的问题:)
我现在选择的解决方案基于C++ 11.Constantin在他的回答中提到的增强库现在是标准的一部分.如果我们用新的字符串类型std :: u16string替换std :: wstring,那么转换将如下所示:
UTF-8到UTF-16
std::string source;
...
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
std::u16string dest = convert.from_bytes(source);
Run Code Online (Sandbox Code Playgroud)
UTF-16到UTF-8
std::u16string source;
...
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>,char16_t> convert;
std::string dest = convert.to_bytes(source);
Run Code Online (Sandbox Code Playgroud)
从其他答案可以看出,该问题有多种方法.这就是为什么我不选择接受的答案.
Con*_*tin 23
您可以utf8_codecvt_facet从Boost序列化库中提取.
他们的用法示例:
typedef wchar_t ucs4_t;
std::locale old_locale;
std::locale utf8_locale(old_locale,new utf8_codecvt_facet<ucs4_t>);
// Set a New global locale
std::locale::global(utf8_locale);
// Send the UCS-4 data out, converting to UTF-8
{
std::wofstream ofs("data.ucd");
ofs.imbue(utf8_locale);
std::copy(ucs4_data.begin(),ucs4_data.end(),
std::ostream_iterator<ucs4_t,ucs4_t>(ofs));
}
// Read the UTF-8 data back in, converting to UCS-4 on the way in
std::vector<ucs4_t> from_file;
{
std::wifstream ifs("data.ucd");
ifs.imbue(utf8_locale);
ucs4_t item = 0;
while (ifs >> item) from_file.push_back(item);
}
Run Code Online (Sandbox Code Playgroud)
在boost源中查找utf8_codecvt_facet.hpp和utf8_codecvt_facet.cpp文件.
Mar*_*som 17
问题定义明确指出8位字符编码是UTF-8.这使得这是一个微不足道的问题; 只需要将一个UTF规范转换为另一个规范就可以了.
只需看看这些维基百科页面上的UTF-8,UTF-16和UTF-32的编码.
原理很简单 - 根据一个UTF规范进行输入并组装一个32位Unicode代码点,然后根据其他规范发出代码点.单个代码点不需要翻译,任何其他字符编码都需要翻译; 这就是使这成为一个简单问题的原因.
这是wchar_tUTF-8转换的快速实现,反之亦然.它假设输入已经正确编码 - 旧句子"Garbage in,garbage out"适用于此处.我相信验证编码最好是作为一个单独的步骤完成.
std::string wchar_to_UTF8(const wchar_t * in)
{
std::string out;
unsigned int codepoint = 0;
for (in; *in != 0; ++in)
{
if (*in >= 0xd800 && *in <= 0xdbff)
codepoint = ((*in - 0xd800) << 10) + 0x10000;
else
{
if (*in >= 0xdc00 && *in <= 0xdfff)
codepoint |= *in - 0xdc00;
else
codepoint = *in;
if (codepoint <= 0x7f)
out.append(1, static_cast<char>(codepoint));
else if (codepoint <= 0x7ff)
{
out.append(1, static_cast<char>(0xc0 | ((codepoint >> 6) & 0x1f)));
out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
}
else if (codepoint <= 0xffff)
{
out.append(1, static_cast<char>(0xe0 | ((codepoint >> 12) & 0x0f)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
}
else
{
out.append(1, static_cast<char>(0xf0 | ((codepoint >> 18) & 0x07)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 12) & 0x3f)));
out.append(1, static_cast<char>(0x80 | ((codepoint >> 6) & 0x3f)));
out.append(1, static_cast<char>(0x80 | (codepoint & 0x3f)));
}
codepoint = 0;
}
}
return out;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码既适用于UTF-16和UTF-32的输入,仅仅是因为范围d800通过dfff无效码点; 它们表明您正在解码UTF-16.如果你知道这wchar_t是32位,那么你可以删除一些代码来优化函数.
std::wstring UTF8_to_wchar(const char * in)
{
std::wstring out;
unsigned int codepoint;
while (*in != 0)
{
unsigned char ch = static_cast<unsigned char>(*in);
if (ch <= 0x7f)
codepoint = ch;
else if (ch <= 0xbf)
codepoint = (codepoint << 6) | (ch & 0x3f);
else if (ch <= 0xdf)
codepoint = ch & 0x1f;
else if (ch <= 0xef)
codepoint = ch & 0x0f;
else
codepoint = ch & 0x07;
++in;
if (((*in & 0xc0) != 0x80) && (codepoint <= 0x10ffff))
{
if (sizeof(wchar_t) > 2)
out.append(1, static_cast<wchar_t>(codepoint));
else if (codepoint > 0xffff)
{
out.append(1, static_cast<wchar_t>(0xd800 + (codepoint >> 10)));
out.append(1, static_cast<wchar_t>(0xdc00 + (codepoint & 0x03ff)));
}
else if (codepoint < 0xd800 || codepoint >= 0xe000)
out.append(1, static_cast<wchar_t>(codepoint));
}
}
return out;
}
Run Code Online (Sandbox Code Playgroud)
再次,如果你知道这wchar_t是32位,你可以从这个函数中删除一些代码,但在这种情况下,它应该没有任何区别.表达式sizeof(wchar_t) > 2在编译时是已知的,因此任何体面的编译器都会识别死代码并将其删除.
Ben*_*aub 12
有几种方法可以做到这一点,但结果取决于字符编码在string和wstring变量中的含义.
如果你知道string是ASCII,你可以简单地使用wstring's iterator构造函数:
string s = "This is surely ASCII.";
wstring w(s.begin(), s.end());
Run Code Online (Sandbox Code Playgroud)
string但是,如果您有其他编码,则会得到非常糟糕的结果.如果编码是Unicode,您可以查看ICU项目,该项目提供了一组跨平台的库,可以转换为各种Unicode编码.
如果你string在代码页中包含了字符,那么$ DEITY可以怜悯你的灵魂.
您可以使用codecvtlocale 方面。定义了一个特定的专业化,codecvt<wchar_t, char, mbstate_t>可能对您有用,但其行为是特定于系统的,并且不保证以任何方式转换为 UTF-8。