本文整理自网络,侵删。
Delphi XE8安卓下汉字转Gb2312 urlencode代码
在Android下汉字转GB2312 urlencode时候,如果使用下边代码:
用Edit2.Text :=Tnetencoding.url.EncodeBytesToString( Tencoding.GetEncoding(936).getbytes('提交'));//会提示错误No mapping for the Unicode character exists in the target multi-byte code page.
用Edit2.Text :=httpencode('提交'); urlencode结果为 %E6%8F%90%E4%BA%A4 ,此为UTF8转换结果
用Edit2.Text :=Tnetencoding.URL.Encode('提交'); urlencode结果同样为%E6%8F%90%E4%BA%A4
那么问题来了,怎么在Android显示GB2312的转换结果%cc%e1%bd%bb呢?
function MyUrlEncode(const input: string): string; // 汉字转Gb2312 urlencode
var
S: string;
Stream: TStringStream;
B: Byte;
begin
Result := '';
S := '';
try
Stream := TStringStream.Create(input, TEncoding.GetEncoding(936));
for B in Stream.Bytes do
S := Format('%s%%%.2x', [S, B]);
finally
Stream.Free;
end;
Result := S;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text := '提交';
Edit2.Text := MyUrlEncode(Edit1.Text); // 结果 %cc%e1%bd%bb
end;
相关阅读 >>
Delphi 万年历 程序源码下部分(包括:农历计算、24节气、星期计算、属相)
更多相关阅读请进入《Delphi》频道 >>