- 人气:
- 放大
- 缩小
- 二维码
- 赞赏
delphi下的纯Pascal的十六进制转十进制
Delphi下的纯Pascal的十六进制转十进制
作者: 小坏
Function StrLenA(Str :PAnsiChar):Integer;
Begin
Result := 0;
while Str[Result] <> #$0 do Inc(Result)
End;
Function Char2Int(A :AnsiChar):Integer;
Begin //字符转整数
Result := -1;
if (Byte(A) > 47) And (Byte(A) < 58) Then
Begin //0-9
Result := Byte(A) - 48;
End Else if (Byte(A) > 64) And (Byte(A) < 71) then
Begin //A-F
Result := Byte(A) - 55;
End Else if (Byte(A) > 96) And (Byte(A) < 103) then
Begin //a-f
Result := Byte(A) - 87;
End;
End;
Function HexPower(X, Y:Integer):UInt64;
Var //次方计算
I :Integer;
Begin
Result := X;
for I := 1 to Y do
Begin
Result := Result * 16;
End;
End;
Function Hex2Int(HEX :PAnsiChar):UInt64;
Var //十六进制字符串转整数
iLen :Integer;
I :Integer;
Begin
iLen := StrLenA(HEX);
Result:= 0;
for I:= 0 to iLen-2 do
Begin
Result := Result + HexPower(Char2Int(HEX[I]), iLen - (I + 1));
End;
Result := Result + Char2Int(HEX[iLen-1]);
End;
代码实例:
Var
HEX :Array [0..16] Of AnsiChar;
begin
HEX := '14f03'#$0;
Writeln(Hex2Int(@HEX));
HEX := 'FFFFFFFF'#$0;
Writeln(Hex2Int(@HEX));
HEX := 'FFFFFFFFFFFFFFFF'#$0;
Writeln(Hex2Int(@HEX));
Readln;
End.