delphi类型转换 asc与char

ord(char) = asc
chr(asc) = char
inttohex(int,1) = hex (string)
 
使用AStr[i]取AStr:String中的第i个字符时需要注意的事项:
这里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;  
 
优化,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;
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值