弱问:向ListView控件添加汉字子项显示问题
"测试邮件 "显示出来变成 " =?gb2312?B?tPC4tDogsuLK1DI=?= "
代码片断:
TListItem *pItem = lvMailList-> Items-> Add();
pItem-> Caption = IntToStr(Proceeding);
pItem-> SubItems-> Add(POP3-> Summary-> From);
pItem-> SubItems-> Add(POP3-> Summary-> Subject);
不知道怎么进行编码转换啊,谢谢指教!
[解决办法]
看那个字符串,貌似Base64编码。
Base64编码由“ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=”组成,所以将 " =?gb2312?B?tPC4tDogsuLK1DI=?= "截取一下,取出 "tPC4tDogsuLK1DI= "这一段(Base64编码是以=结尾),然后用Base64解码函数解一下就行了:
Byte Chr2Base(char c)
{
if(c > = 'A ' && c <= 'Z ')
return(Byte)(c - 'A ');
else if(c > = 'a ' && c <= 'z ')
return(Byte)(c - 'a ' + 26);
else if(c > = '0 ' && c <= '9 ')
return(Byte)(c - '0 ' + 52);
else if(c == '+ ')
return 62;
else
return 63;
}
//---------------------------------------
AnsiString Base64Decode(AnsiString s)
{
int n = s.Length();
Byte c, t;
AnsiString strRet = " ";
for(int i = 1; i <= n; i++)
{
if(s[i] == '= ')
break;
c = Chr2Base(s[i]);
if(i % 4 == 1)
t = c < < 2;
else if(i % 4 == 2)
{
strRet +=(char)(t |(c > > 4));
t =(Byte)(c < < 4);
}
else if(i % 4 == 3)
{
strRet +=(char)(t |(c > > 2));
t =(Byte)(c < < 6);
}
else
strRet +=(char)(t | c);
}
return strRet;
}
//---------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
String str = "tPC4tDogsuLK1DI= ";
ShowMessage(Base64Decode(str));
}