很多时候有收到以 ‘=?utf-8?Q?=’或者 ‘?GB2312?B?1qe’开头的信息,那么如何解码呢
以下是函数:目前增加支持 utf-8,BIG5的转换。
Q表示Q编码,B表示B编码。
=============================
uses EncdDecd
=========================
//信息/Subject: =?utf-8?Q?=E5=9B=BD=E9=99=85=E8=B6=B3=E8=81=94=E9=A6=96=E6=AC=BE=E7=BD=91=E6=B8=B8FIFA_OL2=E5=90=AF=E5=8A=A8=E5=85=AC=E6=B5=8B!=E7=8B=82=E9=80=81adidas=E8=A3=85=E5=A4=87=E5=92=8C=E6=B8=B8=E6=88=8F=E6=89=8B=E6=9F=84?=
//翻译结果/Subject: 国际足联首款网游FIFA OL2启动公测!狂送adidas装备和游戏手柄
function sak_ConvertCharSet( const Line : string ) : string;
var
strToDecode, RestBefore, RestAfter, strActual, decoded,scharset: string;
iLast, iFirst, eFirst, i: integer;
Encoding, c: char;
Dest: TMemoryStream;
begin
strActual := Line;
iFirst := Pos( '=?', strActual );
while (iFirst > 0) do
begin
RestBefore := copy( strActual, 1, iFirst-1 );
strToDecode := copy( strActual, iFirst+2, length( strActual) );
eFirst := pos( '?', strToDecode);
if eFirst > 0 then
begin
scharset:=UpperCase( Copy(strToDecode,1,eFirst-1));
Encoding := UpperCase( strToDecode[eFirst+1])[1];
delete( strToDecode, 1, eFirst + 2); // remove until ?Q? or ?B? inclusive
iLast := Pos( '?=', strToDecode );
if iLast > 0 then
begin
RestAfter := copy( strToDecode, iLast+2, length( strToDecode) );
delete( strToDecode, iLast, length( strToDecode)); // remove the ?= and the rest
strActual := RestBefore + RestAfter;
if Encoding = 'Q' then
begin
strActual := sak_QuotedPrintableDecode( PChar( strToDecode) ) ;
end else
if Encoding = 'B' then
begin
strActual := DecodeString(strToDecode) ;
end;
if scharset='BIG5' then strActual:=BIG52GBK(strActual,950,936)//950:BIG5代码表;936:GBK代码表
else if scharset='UTF-8' then strActual:=UTF8Decode(strActual);
{strActual:=BIG52GBK(strActual,65001,936);//65001:UTF8代码表;936:GBK代码表,但是使用这种办法转换结尾有乱码
}
strActual := RestBefore + strActual + RestAfter;;
iFirst := Pos( '=?', strActual );
end
else iFirst := 0;
end
else iFirst := 0;
end;
Result := strActual;
end;