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

发布于 2024-10-15 01:14:58 字数 306 浏览 4 评论 0原文

我需要解码:

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

How to do it in Delphi before 2009(我使用Delphi 2006)?

I need to decode:

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

into

file://localhost/G:/test/気まぐれロマンティック.mp3

How to do it in Delphi prior 2009 (I use Delphi 2006) ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╭ゆ眷念 2024-10-22 01:14:58

我没有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]:= '

当我使用不同字体测试上述代码时,与问题中的日语字符串相比,名称以“@”开头的字体中的日语符号出现逆时针旋转 90 度。带有 SHIFTJIS_CHARSET 的“Arial Unicode MS”字体提供精确(非旋转)的外观。

; 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”字体提供精确(非旋转)的外观。

I have no Delphi 2006, so I tested the code on Delphi 2007; you should:

convert the string with "%" characters to plain UTF8 string;

convert UTF8 string to Wide String (UTF8Decode);

convert Wide String to Ansi String with Japanese encoding (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]:= '

When I tested the above code with different fonts the Japanese symbols from the fonts with names starting from '@' appeared rotated 90 degrees counterclockwise compared with the Japanese string from the question. The "Arial Unicode MS" font with SHIFTJIS_CHARSET gives exact (non-rotated) appearance.

; 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;

When I tested the above code with different fonts the Japanese symbols from the fonts with names starting from '@' appeared rotated 90 degrees counterclockwise compared with the Japanese string from the question. The "Arial Unicode MS" font with SHIFTJIS_CHARSET gives exact (non-rotated) appearance.

烟织青萝梦 2024-10-22 01:14:58

IdURI 单元中的 Indy TIdURI 类包含 UrlDecode / UrlEncode 函数。您可以使用 Indy 的最新版本(10.5.8)进行尝试,它具有编码参数:

class function TIdURI.URLDecode(ASrc: string; AByteEncoding: TIdTextEncoding = nil
  {$IFDEF STRING_IS_ANSI}; ADestEncoding: TIdTextEncoding = nil{$ENDIF}
  ): string; 

The Indy TIdURI class in IdURI unit contains UrlDecode / UrlEncode functions. You could try it with a recent version (10.5.8) of Indy, which has encoding parameters:

class function TIdURI.URLDecode(ASrc: string; AByteEncoding: TIdTextEncoding = nil
  {$IFDEF STRING_IS_ANSI}; ADestEncoding: TIdTextEncoding = nil{$ENDIF}
  ): string; 
幼儿园老大 2024-10-22 01:14:58

我找到了解决方案,我在这里得到了一个指针: 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'));

I've found the solution, I got a pointer here: delphigroups.info/2/5/209620.html then experiment with HttpDecode in httpApp.pas, the solution is:

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'));
紫瑟鸿黎 2024-10-22 01:14:58

mjn 可能是正确的,但我强烈建议,如果您要在 Delphi 中处理任何类型的 Unicode,请尝试说服您的老板升级到最新版本的 Delphi(或只是 Delphi 2009)。 Delphi 2009 中的 Unicode 支持非常好,并且开箱即用。如果你真的做不到这一点,TNT 组件对我们有用,但最终我们只是等待 Delphi 2009 年才推出,因为从 Borland 开箱即用要简单得多。

mjn may be correct, but I would strongly advise that if you are going to be handling any kind of Unicode within Delphi, try to persuade your bosses to upgrade to the latest version of Delphi (or just Delphi 2009). The Unicode support in Delphi 2009 is very good, and just works out of the box. If you really cant do this, the TNT components worked for us but in the end we just waited for Delphi 2009 to come out because it was so much simpler to have it out of the box from Borland.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文