How to decode url containing Japanese char in Delphi prior 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
i开发者_如何学JAVAnto
file://localhost/G:/test/気まぐれロマンティック.mp3
How to do it in Delphi prior 2009 (I use Delphi 2006) ?
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]:= '$';
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.
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;
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'));
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.
精彩评论