this slowpoke moves

Generate IP Range

Beispiel 1 :
function ToIP(I1,I2,I3,I4: Integer): Cardinal;
  function Check(Value: Integer): Byte;
  begin
    if (Value >= 0) and (Value <= 255) then Result := Value
      else raise Exception.Create('ToIP: Values I1,I2,I3,I4 must be in Range 0 upto 255 each');
  end;
  begin
  Result := Check(I1) shl 24 or Check(I2) shl 16 or Check(I3) shl 8 or Check(I4);
end;

function IPToStr(Value: Cardinal): String;
begin
  Result := Format('%d.%d.%d.%d', [Value shr 24, Value shr 16 and $FF, Value shr 8 and $FF, Value and $FF]);
end;

procedure Range;
var
  IP: Cardinal;
begin
  for IP := ToIP(192,168,2,100) to ToIP(192,168,5,100) do
    //Writeln( IPToStr(IP) );
    Form1.Memo1.Lines.Add(IPToStr(IP))
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Range;
end;
Beispiel 2 :
type
  TIPAddr = array[0..3] of Byte;

function GetNext(var IPAddr: TIPAddr): Boolean;
var
  C: Integer;
begin
  Result := True;
  for C := 3 downto 0 do
  begin
    if IPAddr[C] < 255 then
    begin
      Inc(IPAddr[C]);
      Exit;
    end;
    IPAddr[C] := 0;
  end;
  Result := False;
end;

function IsBelowOrEqual(IP, Limit: TIPAddr): Boolean;
begin
  Result := (IP[0] shl 24 + IP[1] shl 16 + IP[2] shl 8 + IP[3]) <=
    (Limit[0] shl 24 + Limit[1] shl 16 + Limit[2] shl 8 + Limit[3]);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  Start, Stop: TIPAddr;
begin
  Start[0] := 192;
  Start[1] := 168;
  Start[2] := 0;
  start[3] := 1;

  Stop[0] := 192;
  Stop[1] := 168;
  Stop[2] := 1;
  Stop[3] := 10;
  repeat
    ListBox1.Items.Add(IntToStr(Start[0]) + '.' + IntToStr(Start[1]) + '.' +
      IntToStr(Start[2]) + '.' + IntToStr(Start[3]));
    if not GetNext(Start) then
      Exit;
  until not IsBelowOrEqual(Start, Stop);
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate