文章出处:http://hi.baidu.com/myyear/item/cc93de16a606385af1090e99?qq-pf-to=pcqq.temporaryc2c
delphi 中字符串与16进制、10进制转换函数
//字符串转成16进制代码
function strToHexStr(str:string):string;
var
c:char;
ss:string;
i:integer;
begin
while str<>'' do begin
c:=str[1];
ss:=ss+format('%0x',[ord(c)]);
delete(str,1,1);
end;
strtohexStr:= ss;
end;
//16进制字符串转换成字符串
function HexStrToStr(const S:string):string;
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin //xlh 2006.10.21
while (t<=Length(S)) and (not (S[t] in ['0'..'9','A'..'F','a'..'f'])) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
Result:=Result+Chr(M);
inc(t,2);
end;
end;
// 16 进制转换为 10 进制;
//Delphi 中 16 进制数以 $ 为前缀,所以将 16 进制形式的字符串转换为整数即可:
function HexToDec(const AHexString: String): Integer;
begin
Result := StrToInt('$' + AHexString);
end;