Delphi XE8安卓下汉字转Gb2312 urlencode代码(原创)
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;