如何在Delphi 2009之前解码包含日语字符的URL?

3

我需要解码:

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

如何在Delphi 2009之前(我使用的是Delphi 2006)实现此功能?

我找到了解决方案,我在这里得到了一个指针:http://www.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')); - Irwan
4个回答

3

我没有Delphi 2006,所以我在Delphi 2007上测试了代码;你需要:

将带有“%”字符的字符串转换为普通的UTF8字符串;

将UTF8字符串转换为宽字符串(使用UTF8Decode);

将宽字符串转换为日语编码的Ansi字符串(使用WideCharToMultiByte):

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”字体具有确切(未旋转)外观。


2

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; 

2

我已经找到了解决方法,这里有一个指针: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'));

1

mjn 可能是正确的,但我强烈建议如果您将要在 Delphi 中处理任何类型的 Unicode,请劝说您的老板升级到最新版本的 Delphi(或仅使用 Delphi 2009)。Delphi 2009 中的 Unicode 支持非常好,并且可以直接使用。如果您真的无法这样做,TNT 组件 对我们有用,但最终我们只是等待了 Borland 推出 Delphi 2009,因为它从盒子里拿出来要简单得多。


1
直到64位编译器出现才可以 :-) - Irwan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接