0
votes

I use the HTTPEncode() function in Delphi XE8 to encode Japanese text. Some characters can encode correctly, but some cannot. Below is an example:

aStr := HTTPEncode('萩原小学校');

I expected this:

aStr = '%E8%90%A9%E5%8E%9F%E5%B0%8F%E5%AD%A6%E6%A0%A1'

But I got this:

aStr = '%E8%90%A9%E5%8E%9F%E5%B0%8F%3F%E6%A0%A1'

Can someone help me to encode '萩原小学校' as '%E8%90%A9%E5%8E%9F%E5%B0%8F%E5%AD%A6%E6%A0%A1'?

2
Looks like the '学' character is being converted to '?' before being encoded as %3F. The rest of the characters are fine. Are you sure your input string is OK to begin with? Are you saving the source file as UTF-8? Is this an exact copy/paste of your real code?Remy Lebeau
@Remy the deprecated HTTPEncode function takes AnsiString. So perhaps this text can't be encoded in the user's ANSI locale. For me with my UK locale I got 5 ? symbols.David Heffernan
@DavidHeffernan: That may be, though it wouldn't make sense for 5 of the Japanese symbols to encode correctly and only 1 not. Unless the user's Japanese locale doesn't support that 1 symbol. You might want to update your answer to include the fact that HTTPEncode() operates on AnsiString and TNetEncoding.URL.Encode() operates on UnicodeString insteadRemy Lebeau
@RemyLebeau I wasn't even sure what HTTPEncode was. Not least because the behaviour reported in the question seems so odd, as you describe.David Heffernan

2 Answers

4
votes

I'm not sure what this HTTPEncode function is. There is a function in Web.HTTPApp of that name. Perhaps that is what you refer to. If so, it is clearly marked as deprecated. Assuming you have enabled compiler warnings, the compiler will be telling you this, and telling you also to use TNetEncoding.UTL.Encode instead.

Let's try that:

{$APPTYPE CONSOLE}
uses
  System.NetEncoding;

begin
  Writeln(TNetEncoding.URL.Encode('萩原小学校'));
end.

Output

%E8%90%A9%E5%8E%9F%E5%B0%8F%E5%AD%A6%E6%A0%A1
-1
votes

@ David Heffernan, @ Remy Lebeau, thank you so much for your time to help me. your answer make me understand why i cannot convert my string with HTTPEncode.
I have tried many times myself till i found this: Delphi: Convert from windows-1251 to Shift-JIS

function MyEncode(const S: string; const CodePage: Integer): string;
var
  Encoding: TEncoding;
  Bytes: TBytes;
  b: Byte;
  sb: TStringBuilder;
begin
  Encoding := TEncoding.GetEncoding(CodePage);
  try
    Bytes := Encoding.GetBytes(S);
  finally
    Encoding.Free;
  end;

  sb := TStringBuilder.Create;
  try
    for b in Bytes do begin
      sb.Append('%');
      sb.Append(IntToHex(b, 2));
    end;
    Result := sb.ToString;
  finally
    sb.Free;
  end;
end;  

MyEncode('萩原小学校', 65001);
Output = %E8%90%A9%E5%8E%9F%E5%B0%8F%E5%AD%A6%E6%A0%A1