Einzelnen Beitrag anzeigen

hypfvieh

Registriert seit: 1. Jan 2009
2 Beiträge
 
#1

Eine Netzmaske von CIDR in Dezimal umwandeln

  Alt 25. Mär 2009, 13:36
Hallo zusammen,

hier ein wenig Code der bei meinem letzten Projekt abgefallen ist. Vielleicht kanns ja wer brauchen.

Es geht darum aus der CIDR Angabe (/24 zB) von IPv4 Adressen, die entsprechende punktierte Dezimalversion zu
erzeugen (zB 255.255.255.0).

Delphi-Quellcode:

function num_to_ip(IpAsLong : LongWord) : String;
var a,b,c,d : integer;
begin

   a := (IpAsLong shr 24) and $FF;
   b := (IpAsLong shr 16) and $FF;
   c := (IpAsLong shr 8) and $FF;
   d := IpAsLong and $FF;

   Result := inttostr(a) + '.' + inttostr(b) + '.' + inttostr(c) + '.' + inttostr(d);
end;

function cidr_to_netmask(cidr : integer) : string;
var tmp : LongWorD;
begin
    tmp := ( $FFFFFFFF shl (32 - cidr) );
    Result := num_to_ip(tmp);
end;
Aufruf (Beispiel):
Delphi-Quellcode:
   procedure TForm1.Button1Click(Sender: TObject);
   begin
       listbox1.Items.add(cidr_to_netmask(24));
   end;
[edit=Daniel G] Mfg, Daniel G[/edit]
  Mit Zitat antworten Zitat