想从其它数据库中读取整型IP地址,还原成点分十进制,可有的是负数,不知怎么得来的
其中正直可以通过A*256^3+B*256^2+C*256+D算出来
一开始还以为是32位有符号整型,可算出来的值不对
请指教!

解决方案 »

  1.   

    unit Unit11;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm11 = class(TForm)
        edt1: TEdit;
        btn1: TButton;
        edt2: TEdit;
        btn2: TButton;
        procedure btn1Click(Sender: TObject);
        procedure btn2Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        function ip2Int(const strIP: string): Int64;
        function int2Ip(intIP: Int64): string;
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form11: TForm11;implementation{$R *.dfm}function TForm11.int2Ip(intIP : Int64) : string;
    var
      n : int64;
    begin
      Result := '';
      n := intIP shr 24;
      intIP := intIP xor (n shl 24);
      Result := IntToStr(n) + '.';  n := intIP shr 16;
      intIP := intIP xor (n shl 16);
      Result := Result + IntToStr(n) + '.';  n := intIP shr 8;
      intIP := intIP xor (n shl 8);
      Result := Result + IntToStr(n) + '.';  n := intIP;
      Result := Result + IntToStr(n);
    end;function TForm11.ip2Int(const strIP : string): Int64;
    var
      lst : TStringList;
      i : integer;
    begin
      result := 0;
      lst := TStringList.Create;
      try
        lst.Delimiter := '.';
        lst.DelimitedText := strIP;    for i := 0 to lst.Count - 1 do
          result := result + StrToInt64(lst[i]) shl (24 - i * 8);
      finally
        lst.Free;
      end;
    end;procedure TForm11.btn1Click(Sender: TObject);
    begin
      edt2.Text := IntToStr(ip2Int(edt1.Text));
    end;procedure TForm11.btn2Click(Sender: TObject);
    begin
      edt1.Text := int2Ip(StrToInt64(edt2.Text));
    end;procedure TForm11.FormCreate(Sender: TObject);
    begin
      edt1.Text := '192.168.1.1';
      btn1.Click;
    end;end.
      

  2.   

    int 转 uint的问题...  是不是负的有什么问题..
    我只在乎他在内存地址里面是什么值.
      

  3.   

    很简单,ip第一位大于128就是负数
    也就是32bit整数最高位是1。后面的数字的补码就是它的绝对值。
      

  4.   

    另外你要注意bigendian,那么是第四个字节。