求一个可以把16进制字符串转为数值的函数(包括正数和负数)
例: FFFF=-1
    0047=71

解决方案 »

  1.   

    一楼!不是的!
    我要的是字符串FFFF转成数值-1
    用inttostr不可以啊!
      

  2.   


    {十六进制转换为十进制}   
      
    function HexToInt(const aHex: string): Integer;   
      
    var   
      
      I,L,K: Integer;   
      
    begin   
      
      Result := 0;   
      
      if aHex = '' then Exit;   
      
      K := 0;   
      
      L := Length(aHex);   
      
      
      
      for I:=1 to L do  
      
      begin   
      
        if (not(aHex[I] in['A'..'F'])) and (not(aHex[I] in['a'..'f'])) then   
      
          K := K + Trunc(StrToInt(aHex[I]) * Power(16, L-I))   
      
        else  
      
          case aHex[I] of   
      
            'a', 'A' : K := K + Trunc(10 * Power(16, L-I));   
      
            'b', 'B' : K := K + Trunc(11 * Power(16, L-I));   
      
            'c', 'C' : K := K + Trunc(12 * Power(16, L-I));   
      
            'd', 'D' : K := K + Trunc(13 * Power(16, L-I));   
      
            'e', 'E' : K := K + Trunc(14 * Power(16, L-I));   
      
            'f', 'F' : K := K + Trunc(15 * Power(16, L-I));   
      
          end;   
      
      end;   
      
      Result := K;   
      
    end;
      

  3.   

    啊,那就是StrToInt('$'+s); s就是FFFF这些,但是不包括'$'前缀。
      

  4.   

    各位,正数的是可以的!但负数就不是了!
    我需要的是FFFF=-1 而不是FFFF=65535
      

  5.   

    楼主,我想你要的是这样的:function hextoint(s:string):integer;
    var temp:integer;
    begin
    try
    temp:=StrToInt('$'+s);
    except
    showmessage('输入的不是有效16进制数!');
    abort;
    end;
    if temp<=32767
    then result:=temp
    else if temp<65536
    then
    result:=temp-65536
    else 
    begin
    showmessage('数值超出范围!');
    abort;
    end;
    end;
      

  6.   

    没那么复杂。
    smallint(StrToInt('$'+s))就解决了。
      

  7.   

    给个思路吧,太晚了不想写了。你这个就是最前面一位是正负标志位,你自己处理下。再用INTTOSTR不就可以了。