你会认为这很容易获得,但我很难找到一个简单的库函数,它将C或C++字符串从ISO-8859-1编码转换为UTF-8.我正在读取8位ISO-8859-1编码的数据,但需要将其转换为UTF-8字符串,以便在SQLite数据库和最终的Android应用程序中使用.
我发现了一种商业产品,但这次超出了我的预算.
R..*_*R.. 37
如果您的源编码始终是ISO-8859-1,这是微不足道的.这是一个循环:
unsigned char *in, *out;
while (*in)
if (*in<128) *out++=*in++;
else *out++=0xc2+(*in>0xbf), *out++=(*in++&0x3f)+0x80;
Run Code Online (Sandbox Code Playgroud)
为了安全起见,您需要确保输出缓冲区是输入缓冲区的两倍,或者包括大小限制并在循环条件下检查它.
小智 10
对于c ++我使用这个:
std::string iso_8859_1_to_utf8(std::string &str)
{
string strOut;
for (std::string::iterator it = str.begin(); it != str.end(); ++it)
{
uint8_t ch = *it;
if (ch < 0x80) {
strOut.push_back(ch);
}
else {
strOut.push_back(0xc0 | ch >> 6);
strOut.push_back(0x80 | (ch & 0x3f));
}
}
return strOut;
}
Run Code Online (Sandbox Code Playgroud)
您可以使用 boost::locale 库:
http://www.boost.org/doc/libs/1_49_0/libs/locale/doc/html/charset_handling.html
代码如下所示:
#include <boost/locale.hpp>
std::string utf8_string = boost::locale::conv::to_utf<char>(latin1_string,"Latin1");
Run Code Online (Sandbox Code Playgroud)