我一直在尝试使用从以下获得的代码将 ISO-8859 字符集转换为 utf-8: Convert ISO-8859-1 strings to UTF-8 in C/C++ 这是我的代码:
#include <iostream>
#include <string>
using namespace std;
int main(int argc,char* argv[])
{
string fileName ="ħëlö";
int len= fileName.length();
char* in = new char[len+1];
char* out = new char[2*(len+1)];
memset(in,'\0',len+1);
memset(out,'\0',len+1);
memcpy(in,fileName.c_str(),2*(len+1));
while( *in )
{
cout << " ::: " << in ;
if( *in <128 )
{
*out++ = *in++;
}
else
{
*out++ = 0xc2+(*in>0xbf);
*out++ = (*in++&0x3f)+0x80;
}
}
cout << "\n\n out ::: " << out << "\n";
*out = '\0';
}
但输出是
::: ħëlö ::: ?ëlö ::: ëlö ::: ?lö ::: lö ::: ö ::: ?
out :::
输出“out”应该是一个 utf-8 字符串,但它不是。我在 Mac OS X 中得到这个 ..
我在这里做错了什么..?