C++ 如何转换单词'';从utf-8到gbk
这是我的密码。 sz是单词的缩写䶮',sz2是单词的缩写' 它们都是用utf-8编码的。它们在二进制级别上不是同一个字。 将它们转换为GBK中的编码字符集是我想看到的。 但是当sz进行代码转换时,我遇到了一个“转换失败”。 我猜问题是因为他们在boost gbk代码页中没有对应的单词。如果是,我如何解决这个问题。我不会说英语。希望我的描述不会让你感到困惑C++ 如何转换单词'';从utf-8到gbk,c++,boost,C++,Boost,这是我的密码。 sz是单词的缩写䶮',sz2是单词的缩写' 它们都是用utf-8编码的。它们在二进制级别上不是同一个字。 将它们转换为GBK中的编码字符集是我想看到的。 但是当sz进行代码转换时,我遇到了一个“转换失败”。 我猜问题是因为他们在boost gbk代码页中没有对应的单词。如果是,我如何解决这个问题。我不会说英语。希望我的描述不会让你感到困惑 char sz[] = { 0xE4,0xB6, 0xAE,0}; char sz2[] = { 0xEE, 0xA1,0xA3,0 };
char sz[] = { 0xE4,0xB6, 0xAE,0};
char sz2[] = { 0xEE, 0xA1,0xA3,0 };
fstream o("1.dat", std::ios::out | std::ios::binary);
try {
o << boost::locale::conv::from_utf(sz2, "gbk", boost::locale::conv::stop) << endl;
o << boost::locale::conv::from_utf(sz, "gbk", boost::locale::conv::stop) << endl;
}
catch(boost::locale::conv::conversion_error e){
cout << e.what() << endl;
}
o.close();
charsz[]={0xE4,0xB6,0xAE,0};
char sz2[]={0xEE,0xA1,0xA3,0};
fstream o(“1.dat”,std::ios::out | std::ios::binary);
试一试{
o
这是我的密码。sz是单词的缩写䶮',sz2是单词的缩写' 他们都用utf-8编码
第二个是完全未知的:(另见)
当然,如果输入使用非标准/未知的代码点,这会丢失信息。如果GBK没有相应的符号,则无法正确转换。boost::locale::conv::stop
表示出错时停止,boost::locale::conv::skip
将跳过(忽略)
'.另外,我刚刚了解到GB18030支持整个Unicode空间,是GBK的超集。您可能有兴趣在GBK上使用它。
Character: U+E863
Name: <Private Use>
General Character Properties
Block: Private Use Area
Unicode category: Other, Private Use
#include <fstream>
#include <iostream>
#include <boost/locale.hpp>
int main() {
boost::locale::generator gen;
auto CN = gen.generate("zh_CN.GBK");
for (std::string const input : {
std::string { static_cast<char>(0xE4), static_cast<char>(0xB6), static_cast<char>(0xAE), 0 },
std::string { static_cast<char>(0xEE), static_cast<char>(0xA1), static_cast<char>(0xA3), 0 },
})
{
std::ofstream o("1.dat", std::ios::binary);
o.imbue(CN);
try {
o << boost::locale::conv::from_utf(input, "GBK", boost::locale::conv::skip) << "\n";
std::cout << "Conversion ok!\n";
}
catch(boost::locale::conv::conversion_error e){
std::cout << e.what() << "\n";
}
}
}
Conversion ok!
Conversion ok!