本文整理自网络,侵删。
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呢?
参考代码
Delphi/Pascal
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;
来源:https://www.amingstudio.com/delphi/171.html
相关阅读 >>
Delphi simple resource api replacement
Delphi的tclientsocket组件和tserversocket组件(c/s)说明
Delphi 怎样判断spinedit在微调时值是增加还是减少了
更多相关阅读请进入《Delphi》频道 >>