function StringToHex(sSrcStr:string):string; var //从XE5的帮助中,抄来的例子 LStr1, LStr2: String; //Knight.Chen begin { Store the text in the memo to a String variable. } LStr1 := sSrcStr; { Set the length of the String to hold the conversion. } SetLength(LStr2, Length(LStr1) * 4); { Call the binary to hexadecimal conversion procedure. } BinToHex(LStr1[1], PWideChar(LStr2), Length(LStr1) * SizeOf(Char)); { Put the results in Memo2. } Result := LStr2; end;
Windows下运行结果: StringToHex('目')='EE76' //Unicode编码
---------- XE10.Android
function StringToHex(const Value: string): string; var Length: Integer; InBytes, OutBytes: TBytes; begin InBytes := TEncoding.ANSI.GetBytes(Value); Length := System.Length(InBytes) shl 1; SetLength(OutBytes, Length); System.Classes.BinToHex(InBytes, 0, OutBytes, 0, Length shr 1); Result := TEncoding.ANSI.GetString(OutBytes); end;
procedure Tfrm1.btToHexClick(Sender: TObject); var LStr1, LStr2: String;
begin { Store the text in the memo to a String variable. } LStr1 := Memo1.Lines.Text; { Set the length of the String to hold the conversion. } SetLength(LStr2, Length(LStr1) * 4); { Call the binary to hexadecimal conversion procedure. } BinToHex(LStr1[1], PWideChar(LStr2), Length(LStr1) * SizeOf(Char)); { Put the results in Memo2. } Memo2.Lines.Text := LStr2; end;
----------------------------------------------
...