我需要解码:
file://localhost/G:/test/%E6%B0%97%E3%81%BE%E3%81%90%E3%82%8C%E3%83%AD%E3%83%9E%E3%83%B3%E3%83%86%E3%82%A3%E3%83%83%E3%82%AF.mp3
进入
file://localhost/G:/test/気まぐれロマンティック.mp3
如何在 2009 年之前的 Delphi 中做到这一点(我使用 Delphi 2006)?
我没有Delphi 2006,所以我在Delphi 2007上测试了代码;你应该:
将带有“%”字符的字符串转换为纯 UTF8 字符串;
将 UTF8 字符串转换为宽字符串(UTF8Decode);
使用日语编码 (WideCharToMultiByte) 将宽字符串转换为 Ansi 字符串:
const
SrcStr = 'file://localhost/G:/test/%E6%B0%97%E3%81%BE%E3%81%90%E3%82%8C%E3%83%AD%E3%83%9E%E3%83%B3%E3%83%86%E3%82%A3%E3%83%83%E3%82%AF.mp3';
function Src2Utf8(const S: string): string;
var
I: Integer;
S1: string;
B: Byte;
begin
I:= 0;
Result:= '';
SetLength(S1, 3);
S1[1]:= '$';
while I < Length(S) do begin
Inc(I);
if S[I] <> Char('%') then Result:= Result + S[I]
else begin
Inc(I);
S1[2]:= S[I];
Inc(I);
S1[3]:= S[I];
B:= StrToInt(S1);
Result:= Result + Char(B);
end;
end;
end;
procedure TForm8.Button1Click(Sender: TObject);
var
S: WideString;
S1: string;
begin
S:= Utf8Decode(Src2Utf8(SrcStr));
SetLength(S1, 4 * Length(S)); // more than enough
FillChar(PChar(S1)^, Length(S1), 0);
WideCharToMultiByte(932 {shift-jis codepage}, 0, PWideChar(S), Length(S),
PChar(S1), Length(S1), nil, nil);
S1:= PChar(S1); // to remove ending zeroes
Label1.Caption:= S1;
end;
当我用不同的字体测试上述代码时,名称以“@”开头的字体中的日文符号与问题中的日文字符串相比逆时针旋转了 90 度。带有 SHIFTJIS_CHARSET 的“Arial Unicode MS”字体给出了精确(非旋转)的外观。
我找到了解决方案,我在这里得到了一个指针:delphigroups.info/2/5/209620.html然后在httpApp.pas中尝试使用HttpDecode,解决方案是:
TntEdit2.Text := UTF8Decode(HTTPDecode('file://localhost/G:/test/%E6%B0%97%E3%81%BE%E3%81%90%E3%82%8C%E3%83%AD%E3%83%9E%E3%83%B3%E3%83%86%E3%82%A3%E3%83%83%E3%82%AF.mp3'));
IdURI 单元中的 Indy TIdURI 类包含 UrlDecode / UrlEncode 函数。您可以尝试使用具有编码参数的最新版本 (10.5.8) 的 Indy:
class function TIdURI.URLDecode(ASrc: string; AByteEncoding: TIdTextEncoding = nil
{$IFDEF STRING_IS_ANSI}; ADestEncoding: TIdTextEncoding = nil{$ENDIF}
): string;
mjn 可能是正确的,但我强烈建议如果您要在 Delphi 中处理任何类型的 Unicode,请尝试说服您的老板升级到最新版本的 Delphi(或只是 Delphi 2009)。Delphi 2009 中的 Unicode 支持非常好,开箱即用。如果你真的不能这样做,TNT 组件对我们有用,但最后我们只是等待 Delphi 2009 出来,因为从 Borland 开箱即用要简单得多。