我必须将一些UTF-8编码的文本文件导入到我的C ++ Builder 5程序中。 是否有任何组件或代码示例可以实现?
答案 0 :(得分:2)
你最好阅读有关标记为unicode和c ++的所有其他问题。对于初学者,您应该查看this one并查看已接受答案(UTF8-CPP)中的库是否适合您。
然而,我会先考虑你想要实现的目标,因为你无法将UTF-8编码的字符串导入“Ansi”(你的意思是什么,可能是像ISO8859_1或WIN1252编码?)。
答案 1 :(得分:2)
以下是一种更加以VCL为中心的方法:
UTF8String utf8 = "...";
WideString utf16;
AnsiString latin1;
int len = ::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), utf8.Length(), NULL, 0);
utf16.SetLength(len);
::MultiByteToWideChar(CP_UTF8, 0, utf8.c_str(), utf8.Length(), utf16.c_bstr(), len);
len = ::WideCharToMultiByte(1252, 0, utf16.c_bstr(), utf16.Length(), NULL, 0, NULL, NULL);
latin1.SetLength(len);
::WideCharToMultiByte(1252, 0, utf16.c_bstr(), utf16.Length(), latin1.c_str(), len, NULL, NULL);
如果您升级到CB2009,您可以将其简化为:
UTF8String utf8 = "...";
AnsiString<1252> latin1 = utf8;
答案 2 :(得分:0)
由于没有人在周末工作,我必须自己回答:)
String Utf8ToWinLatin1(char* aData, char* aValue)
{
int i=0;
for(int j=0;j<strlen(aData);)
{ int val=aData[j];
int c=(unsigned char)aData[j];
if(c<=127)
{ aValue[i]=c;
j+=1;
i++;
}
else if(c>=192 && c<=223)
{
aValue[i]=(c-192)*64 + (aData[j+1]-128);
i++;
j+=2;
}
else if(c>=224 && c<=239)
{
aValue[i]=( c-224)*4096 + (aData[j+1]-128)*64 + (aData[j+2]-128);
i++;
j+=3;
}
else if(c>=240 && c<=247)
{
aValue[i]=(c-240)*262144 + (aData[j+1]-128)*4096 + (aData[j+2]-128)*64 + (aData[j+3]-128);
i++;
j+=4;
}
else if(c>=248 && c<=251)
{
aValue[i]=(c-248)*16777216 + (aData[j+1]-128)*262144+ (aData[j+2]-128)*4096 + (aData[j+3]-128)*64 + (aData[j+4]-128);
i++;
j+=5;
}
else
j+=1;
}
return aValue;
}
答案 3 :(得分:-1)
您的问题没有具体说明要转换为哪个字符集。如果您只需要基本的7位ASCII字符集,则丢弃值高于127的每个字符都可以。
如果你想转换成8位字符集,比如latin1,你就必须这么做。