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判断字符串是否为数字

Delphi 62进制转10进制

Delphi services允许跨域访问

Delphi combobox 下拉事件

Delphi 高权限进程实现拖曳文件操作

Delphi计算md5

Delphi 测试 tstreamwriter、tstreamreader

Delphi tidhttp 超时的解决方案

Delphi通过adoquery控件实现sqlserver数据库多结果集的数据打印

Delphi xe10实现移动端支付宝、微信支付接口

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



打赏

取消

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

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

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

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

评论

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