How to decode url containing Japanese char in Delphi before 2009?

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

at

file://localhost/G:/test/ζ°—γΎγγ‚Œγƒ­γƒžγƒ³γƒ†γ‚£γƒƒγ‚―.mp3

How to do this in Delphi until 2009 (I use Delphi 2006)?

+3
source share
4 answers

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

convert a string with the characters "%" to a regular UTF8 string;

convert a UTF8 string to a broadcast string (UTF8Decode);

converts 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]:= '$';
  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 . "Arial Unicode MS" SHIFTJIS_CHARSET () .

+3

Indy TIdURI IdURI 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, HttpDecode httpApp.pas, :

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'));
+2

mjn may be correct, but I would strongly advise that if you are going to handle any Unicode in Delphi, try to convince your bosses to upgrade to the latest version of Delphi (or just Delphi 2009). Unicode support in Delphi 2009 is very good and just works out of the box. If you really can’t do this, the TNT components worked for us, but in the end we were just waiting for Delphi 2009 to come out because it was so much easier to make it out of the box from Borland.

+1
source

Source: https://habr.com/ru/post/1788661/


All Articles