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.

相关阅读 >>

Delphi checklistbox用法

Delphi indy tidhttp tidhttpserver post get

Delphi httpget 判断链接是否可以访问

Delphi 服务器与客户端的时间同步

Delphi android拍照报错

Delphi fmx 切换窗体最大化

Delphi 系统服务和普通forms程序共存一体的实现

Delphi xe8中的firemonkey应用程序将文本复制到剪贴板

Delphi xe2-firemonkey 新功能

Delphi中 tstringlist和thashedstringlist的性能对比

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...