ord(char) = asc
chr(asc) = char
inttohex(int,1) = hex (string)
使用AStr[i]取AStr:String中的第i个字符时需要注意的事项:
这里i表示第i个字符,并不是通常的0表示第1个,i表示第i+1个。
这里i表示第i个字符,并不是通常的0表示第1个,i表示第i+1个。
二位的16进制转换为10进制:
function HexToInt(hex : string):integer;
var x : array [0..1] of integer;
i : integer;
s : string;
begin
for I := 0 to 1 do
begin
try
if i = 0 then
s := copy(hex,1,1)
else
s := copy(hex,2,1);
x[i] := StrToInt(s);
except
if s = 'A' then x[i] := 10;
if s = 'B' then x[i] := 11;
if s = 'C' then x[i] := 12;
if s = 'D' then x[i] := 13;
if s = 'E' then x[i] := 14;
if s = 'F' then x[i] := 15;
end;
end;
Result := x[0] * 16 + x[1];
end;
var x : array [0..1] of integer;
i : integer;
s : string;
begin
for I := 0 to 1 do
begin
try
if i = 0 then
s := copy(hex,1,1)
else
s := copy(hex,2,1);
x[i] := StrToInt(s);
except
if s = 'A' then x[i] := 10;
if s = 'B' then x[i] := 11;
if s = 'C' then x[i] := 12;
if s = 'D' then x[i] := 13;
if s = 'E' then x[i] := 14;
if s = 'F' then x[i] := 15;
end;
end;
Result := x[0] * 16 + x[1];
end;
优化,16 to 10
function HexToInt(Hex : String) : int64;
var AStr, AHex : String;
HexLen, i, AH : integer;
Power : integer;
begin
AHex := AnsiUpperCase(Hex);
HexLen := Length(AHex);
Result := 0;
Power := 1;
for I := 0 to HexLen - 1 do
begin
AStr := Copy(AHex,HexLen - i); //从后往前取
AH := Ord(AStr[1]) - 48;
if (AH >= 17) and (AH <= 22) then AH := AH -7
else if (AH < 0) or (AH > 22) or ((AH > 9) and (AH < 17)) then AH := null;
if i <> 0 then Power := Power * 16;
Result := Result + AH * Power;
end;
end;
var AStr, AHex : String;
HexLen, i, AH : integer;
Power : integer;
begin
AHex := AnsiUpperCase(Hex);
HexLen := Length(AHex);
Result := 0;
Power := 1;
for I := 0 to HexLen - 1 do
begin
AStr := Copy(AHex,HexLen - i); //从后往前取
AH := Ord(AStr[1]) - 48;
if (AH >= 17) and (AH <= 22) then AH := AH -7
else if (AH < 0) or (AH > 22) or ((AH > 9) and (AH < 17)) then AH := null;
if i <> 0 then Power := Power * 16;
Result := Result + AH * Power;
end;
end;
原文来自: http://blog.chinaunix.net/uid/30148519.html
http://blog.chinaunix.net/uid-30148519-id-4850824.html