delphi 知道ip地址和掩码,如何得到网段的第一个地址和最后一个地址

efly888 2019-03-19 09:53:56
根据ip地址和掩码,获得子网的取值范围。
...全文
279 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
BlueStorm 2019-03-20
  • 打赏
  • 举报
回复

Function GetIPRange(const IP, Mask: String): String;
  function IPToVaule(const IP: String): Cardinal;
  var
    Str: String;
    I, P1, P2: Integer;
  begin
    P1 := 1;
    Result := 0;
    for I := 1 to 4 do
    begin
      if I < 4 then
        P2 := Pos('.', IP, P1)
      else
        P2 := Length(IP) + 1;
      Str := Copy(IP, P1 , P2 - P1);
      Result := (Result Shl 8) + Byte(StrToInt(Str));
      P1 := P2 + 1;
    end;
  end;

  function VauleToIP(const Value: Cardinal): String;
  var
    PB: PByte;
    I: Integer;
  begin
    Result := '';
    PB := @Value;
    for I := 3 downto 0 do
    begin
      Result := Result + IntToStr(PB[I]);
      if I > 0 then Result := Result + '.';
    end;
  end;

var
  IPValue, MaskValue, FirstIPValue, LastIPValue: Cardinal;
begin
  IPValue   := IPToVaule(IP  );
  MaskValue := IPToVaule(Mask);
  FirstIPValue := IPValue and MaskValue;
  LastIPValue  := FirstIPValue + (not MaskValue);
  Result := VauleToIP(FirstIPValue) + ' - ' + VauleToIP(LastIPValue );
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  ShowMessage(GetIPRange('192.168.180.101', '255.255.192.0'));
end;
天行归来 2019-03-20
  • 打赏
  • 举报
回复

type
{ 定义IP结构 }
TIp = record
a1 : BYTE;
a2 : BYTE;
a3 : BYTE;
a4 : BYTE;
end;

function longToIP(IntIp: Cardinal; BigEndian: Boolean): string;
var
Ip: TIp;
begin
Ip := TIp(IntIp);
case BigEndian of
False : Result := Format('%d.%d.%d.%d', [Ip.a4, Ip.a3, Ip.a2, Ip.a1]);
True : Result := Format('%d.%d.%d.%d', [Ip.a1, Ip.a2, Ip.a3, Ip.a4]);
end;
end;

//聚合IP转起止IP
function cidrIpToIpList(ip: string; mask: integer): TStrings;
var
i,n: integer;
startIp: Cardinal;
begin
startIp:= IpToLong(ip);
result := TStringList.Create;
n := Round(Power(2,32-mask));
result.Add(longToIp(startIp+0));
result.Add(longToIp(startIp+n-1));
end;


另外,我有写个工具可以实现IP聚合以及IP查询,需要可以下载(本想0下载分,可惜CSDN不让设置)
下载地址:https://download.csdn.net/download/lynmison/10953925
efly888 2019-03-20
  • 打赏
  • 举报
回复
可以了,谢谢bluestorm,也谢谢天行归来。
lao_yunger 2019-03-20
  • 打赏
  • 举报
回复
0和255有特别含义,不能算在内。


BlueStorm 2019-03-20
  • 打赏
  • 举报
回复

  function VauleToIP(const Value: Cardinal): String;
  var
    Bytes: array[0..3] of Byte;
  begin
    Move(Value, Bytes, 4);
    Result := Format('%d.%d.%d.%d',[Bytes[3], Bytes[2], Bytes[1], Bytes[0]]);
  end;
efly888 2019-03-20
  • 打赏
  • 举报
回复
我用的delphi7
efly888 2019-03-20
  • 打赏
  • 举报
回复
Result := Result + IntToStr((PB+I)^); 提示,operator not applicable to this operand type
BlueStorm 2019-03-20
  • 打赏
  • 举报
回复
你的Delphi版本太旧了,改成以下方式应该就没问题了:

function GetIPRange(const IP, Mask: String): String;
  function IPToVaule(IP: String): Cardinal;
  var
    Str: String;
    I, Len, P: Integer;
  begin
    Len := Length(IP);
    Result := 0;
    for I := 1 to 4 do
    begin
      if I < 4 then
        P := Pos('.', IP)
      else
        P := Length(IP) + 1;
      Str := Copy(IP, 1, P-1);
      Result := (Result Shl 8) + Byte(StrToInt(Str));
      IP := Copy(IP, P+1, Len);
    end;
  end;

  function VauleToIP(const Value: Cardinal): String;
  var
    PB: PByte;
    I: Integer;
  begin
    Result := '';
    PB := @Value;
    for I := 3 downto 0 do
    begin
      Result := Result + IntToStr((PB+I)^);
      if I > 0 then Result := Result + '.';
    end;
  end;

var
  IPValue, MaskValue, FirstIPValue, LastIPValue: Cardinal;
begin
  IPValue   := IPToVaule(IP  );
  MaskValue := IPToVaule(Mask);
  FirstIPValue := IPValue and MaskValue;
  LastIPValue  := FirstIPValue + (not MaskValue);
  Result := VauleToIP(FirstIPValue) + ' - ' + VauleToIP(LastIPValue );
end;

procedure TForm3.FormCreate(Sender: TObject);
begin
  ShowMessage(GetIPRange('192.168.180.101', '255.255.192.0'));
end;

efly888 2019-03-20
  • 打赏
  • 举报
回复
谢谢,试了一下@bluestorm的程序,提示P2 := Pos('.', IP, P1)有问题,我把p1删掉了,可以吗?还有Result := Result + IntToStr(PB[I]);这条报 array type required,不知怎么解决?

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧