如何判断一个ip是否在一段ip之间?
比如:
220.123.21.33 是否在  220.122.1.2 ~ 221.1.122.21之间为true;

解决方案 »

  1.   

    uses winsockInet_Addr(pchar('220.123.21.33'));得到一個整數比較大小就可!
      

  2.   

    你是不是要在程序中完成?
    用其他工具可以吗?
    用ipscan.exe可以帮你吗?
      

  3.   

    ari老大, 谢了, 不过在从数据库里取数据(ip)进行这样运算时, 有时值为负数为什么呢?
      

  4.   

    别使用integer,他有符号的。
    使用DWORD,就不会出现负值了
      

  5.   

    >>有时值为负数为什么呢?
    就我所知道, 應該為一正整數才對啊! 負數應該是你的數據有問題吧!
      

  6.   

    showmessage(inttostr(Inet_Addr(pchar('211.162.233.233'))));就为负数
      

  7.   

    耙子,你好, 你的回复:
    别使用integer,他有符号的。
    使用DWORD,就不会出现负值了
    Inet_Addr(pchar('220.123.21.33'));这样的函数如何让它的值为dword呢?
      

  8.   

    我測試了下, 真的有, 那用如下:
     showmessage(inttostr(DWord(Inet_Addr(pchar('211.162.233.233')))));
      

  9.   

    负数是没有了,可问题好象更严重了:
    showmessage(inttostr(DWord(Inet_Addr(pchar('211.162.002.131')))));
    showmessage(inttostr(DWord(Inet_Addr(pchar('211.162.3.120')))));
    看看哪个大???
      

  10.   

    Result := ('220.123.21.33'> '220.122.1.2') and ('220.123.21.33' < '221.1.122.21');
      

  11.   

    真想不到, 原以為很簡單的問題, 卻...
    Inet_Addr 得到的數是低位在前的, 所以, 才會出現你說的問題,

    showmessage(inttohex(Inet_Addr(pchar('211.162.3.120')),2)));
      

  12.   

    对,intel的处理器是低位在前,高位在后。自己写一个交换字节的函数function SwapDWORD(const dwX: DWORD):DWORD;
    begin
      result:= (dwX and $ff000000) shr 24
              +(dwX and $00ff0000) shr 8
              +(dwX and $0000ff00) shl 8
              +(dwX and $000000ff) shl 24;
    end;
    var
      dwBeginIP, dwEndIP, dwIP;  dwBeginIP:= SwapDWORD(Inet_Addr(pchar('211.162.3.1')));
      dwEndIP:= SwapDWORD(Inet_Addr(pchar('211.162.3.255')));
      dwIP:= SwapDWORD(Inet_Addr(pchar('211.162.003.123')));if (dwIP >= dwEndIP) and (dwIP <= dwBegin) then
      .....这样该差不多了