function T16210(k: string): integer; //十六进制字符串转换为十进制数
begin
result := StrtoInt('$' + k);
end;
function T1022(i:integer):string;   //10进制转2进制
var
j: integer;
s: string;
begin
j := i;
s := ' ';
while j >= 2 do
begin
   if (j mod 2) = 1 then
   begin
     s := '1' + s;
     j := j div 2;
   end
   else
   begin
     s := '0' + s;
     j := j div 2;
   end;
end;
s := chr(ord('0') + j) + s;
result := s;
end;procedure TForm1.Button1Click(Sender: TObject);
var str:string;
begin
  str:=inttostr(T16210(edit1.Text));
  edit1.Text:= str;
end;输入小一点的数字比如 f,ff,1234,abc,都可以正常转化
可是我要转化的是比较大的数字.比如以下几组数字,
要转化的16进制数
8524B5C8
8524B5C7
8524B5C6
转化结果(明显就不对)
-2061236984
-2061236985
-2061236986
这是什么原因了,得用什么办法解决啊?

解决方案 »

  1.   

    溢出了。换StrToInt64,如果还不行,那就不能这样处理了,要自己写大数字计算的函数了。function T16210(k: string): integer; //十六进制字符串转换为十进制数 
    begin 
        result := StrtoInt64('$' + k); 
    end; 
      

  2.   

    Type           Range           Format
    Integer -2147483648..2147483647 signed 32-bit
    Int64 -2^63..2^63-1 signed 64-bit
      

  3.   

    谢谢,我用了int64存储.现在16进制转化成10进制是没有问题.
    但是10进制转化成2进制好像是又溢出了.procedure TForm1.Button5Click(Sender: TObject);
    begin
       edit5.Text:=T1022(strtoint64('$'+edit5.Text));
    end;
    测试18524B5C8结果
    ?(就显示一个?号)
    ----------------------------------
    测试2
    ffffffff结果也是?号按理说,自定义函数10进制转二进制,不是把他当作字符串处理了吗,.应该是那个j溢出了吧.int64也装不下有什么好办法解决吗?
      

  4.   


    你的函数,T1022参数是Integer,所以,这里就溢出了。
      

  5.   

    我把T1022参数改成int64了.
    int64不是可以存储 -2^63..2^63-1 signed 64-bit按理说,足够存储的哈,
    我改string也试过了,都溢出了.怎么回事了
      

  6.   

    就用 ffffffff  来测试转化成10进制可以 值为4294967295转化二进制的时候溢出了.
      

  7.   

    返回值不能是有符号的Integer,改用LongWord之类的。
      

  8.   


    你函数里定义的j:integer;
    而j:=i;就已经溢出了,改成 j:int64;
      

  9.   

    function T1022(i:integer):widestring;   //10进制转2进制
    var
    j: int64;
    s: string;
    begin
    j := i;
    s := ' ';
    while j >= 2 do
    begin
       if (j mod 2) = 1 then
       begin
         s := '1' + s;
         j := j div 2;
       end
       else
       begin
         s := '0' + s;
         j := j div 2;
       end;
    end;
    s := chr(ord('0') + j) + s;
    result := s;
    end;
    我把修改后的自定义函数帖一下吧函数返回值的类型也改成int64试过了,结果都一样啊.
      

  10.   

    function T16210(k: string): Int64; //十六进制字符串转换为十进制数
    begin
    result := StrToInt64('$' + k);
    end;
    function T1022(i:Int64):string;  //10进制转2进制
    var
    j: int64;
    s: string;
    begin
    j := i;
    s := ' ';
    while j >= 2 do
    begin
      if (j mod 2) = 1 then
      begin
        s := '1' + s;
        j := j div 2;
      end
      else
      begin
        s := '0' + s;
        j := j div 2;
      end;
    end;
    s := chr(ord('0') + j) + s;
    result := s;
    end;
      

  11.   


    你好,
    改成longword之后不是还得转成string才能输出到edit框中.
    IntToStr(Int64(...)).查了资料,说是这样转,试了不行呃.
      

  12.   

    function HexToBinText(sIn: String): String;
    const
      HEX_BINSTR_Prefx: array [0..15] of String[4] = (  '0000'
                                                      , '0001'
                                                      , '0010'
                                                      , '0011'
                                                      , '0100'
                                                      , '0101'
                                                      , '0110'
                                                      , '0111'
                                                      , '1000'
                                                      , '1001'
                                                      , '1010'
                                                      , '1011'
                                                      , '1100'
                                                      , '1101'
                                                      , '1110'
                                                      , '1111');
      HEX_BINSTR: array [0..15] of String[4] = (  ''
                                                , '1'
                                                , '10'
                                                , '11'
                                                , '100'
                                                , '101'
                                                , '110'
                                                , '111'
                                                , '1000'
                                                , '1001'
                                                , '1010'
                                                , '1011'
                                                , '1100'
                                                , '1101'
                                                , '1110'
                                                , '1111');
    var
      sOut: String;
      pIn,pOut: Pchar;
      Index, I, iCount, iLen: Integer;
    begin
      Result := '';
      if sIn = '' then Exit;
      I := Length(sIn);
      pIn := PChar(sIn);
      iCount := I * 4;
      case pIn^ of
        '0':Dec(iCount, 4);
        '1':Dec(iCount, 3);
        '2','3':Dec(iCount, 2);
        '4','5','6','7':Dec(iCount, 1);
        '8','9','A'..'F','a'..'f':
        else
          Exit;
      end;
      SetLength(sOut, iCount);
      pOut := PChar(sOut);
      Index := 0;
      case pIn^ of
        '0':;
        '1'..'9':Index := Ord(pIn^) - Ord('0');
        'A'..'F':Index := Ord(pIn^) - Ord('A') + 10;
        'a'..'f':Index := Ord(pIn^) - Ord('a') + 10;
      end;
      if Index <> 0 then begin
        iLen := Length(HEX_BINSTR[Index]);
        Move(PChar(HEX_BINSTR[Index])^, pOut^, iLen);
        Inc(pOut, iLen);
      end;  Inc(pIn);
      Dec(I);  while I > 0 do begin
        case pIn^ of
          '0'..'9':Index := Ord(pIn^) - Ord('0');
          'A'..'F':Index := Ord(pIn^) - Ord('A') + 10;
          'a'..'f':Index := Ord(pIn^) - Ord('a') + 10
          else
            Exit;
        end;
        iLen := Length(HEX_BINSTR_Prefx [Index]);
        Move(PChar(HEX_BINSTR_Prefx[Index])^, pOut^, iLen);
        Inc(pOut, iLen);
        Inc(pIn);
        Dec(I);
      end;
      Result := sOut;
    end;
      

  13.   

    不可能啊,我试过是正确的。Edit2.Text:=T1022(StrToInt64('$'+Edit1.Text));输入 8524B5C8
    输出 10000101001001001011010111001000 
      

  14.   

    把const当中的array [0..15] of String[4]改为array [0..15] of String  ShowMessage(HexToBinText(IntToHex($1fff89,6)));
      

  15.   

    非常感谢两位的耐心解答.问题已解决.尤其是gzmhero  
      

  16.   

    对于二进制,八进制,十六进制等这些转换,有直接的对应关系,完全没有必要再使用StrToInt转换一次。