Ver Mensaje Individual
  #3  
Antiguo 22-08-2014
Avatar de ecfisa
ecfisa ecfisa is offline
Moderador
 
Registrado: dic 2005
Ubicación: Tres Arroyos, Argentina
Posts: 10.508
Reputación: 36
ecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to beholdecfisa is a splendid one to behold
Hola rchavezh.

Es como te explica juanelo, te pongo un código de aproximación como ejemplo:
Código Delphi [-]
....
type
  TNetClass = (A, B, C);

const
  NETCLASS: array[TNetClass] of Byte = (8, 16, 24);

procedure TForm1.FormCreate(Sender: TObject);
begin
  EditIP1.Text:= '163.209.141.0';
  EditIP2.Text:= '163.209.142.23';
  EditMask1.Text:= '255.255.255.0';
  EditMask2.Text:= '255.255.255.0';
end;

function IPToBin(const IP: string): string;
  function ToBin(Value: Integer): string;
  var
    i: Integer;
  begin
    Result:= '';
    for i:= 7 downto 0 do
      Result := Result+ Chr(Ord('0')+ (Value shr i and 1));
  end;
var
  TS: TStrings;
  i: Integer;
begin
  Result:= '';
  TS:= TStringList.Create;
  try
    TS.Delimiter:= '.';
    TS.DelimitedText:= IP;
    for i:= 0 to TS.Count-1 do
      Result:= Result + ToBin(StrToInt(TS[i]))+'.';
    SetLength(Result, Length(Result)-1);
  finally
    TS.Free;
  end;
end;
function ProcessIP(ip, ms: string): string;
var
  TS: TStrings;
  v1,v2: array[0..3] of Integer;
  i: Integer;
begin
  TS:= TStringList.Create;
  try
    TS.Delimiter:= '.';
    TS.DelimitedText:= ip;
    for i:= 0 to 3 do v1[i]:= StrToInt(TS[i]);
    TS.DelimitedText:= ms;
    for i:= 0 to 3 do v2[i]:= StrToInt(TS[i]);
    for i:= 0 to 3 do Result:= Result +IntToStr(v1[i] and v2[i]) + '.';
    SetLength(Result,Length(Result)-1);
  finally
    TS.Free;
  end;
end;

function SameNetwork(net1, net2: string; const submask: Integer): Boolean;
var
  i: Integer;
begin
  Result:= True;
  net1:= StringReplace(net1, '.', '', [rfReplaceAll]);
  net2:= StringReplace(net2, '.', '', [rfReplaceAll]);
  for i:= 1 to submask do
  begin
    if net1[i]<>net2[i] then
    begin
      Result:= False;
      Exit;
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  r1, r2: string;
begin
  r1:= IPToBin(ProcessIP(EditIP1.Text, EditMask1.Text));
  r2:= IPToBin(ProcessIP(EditIP2.Text, EditMask2.Text));
  if SameNetwork(r1, r2, NETCLASS[C]) then
    ShowMessage('IP2 pertenece a la misma subred clase C')
  else
    ShowMessage('IP2 no pertenece a misma subred clase C'); //<--
  // Sin embargo,
  if SameNetwork(r1, r2, NETCLASS[b]) then
    ShowMessage('IP2 pertenece a la misma subred clase B')  //<--
  else
    ShowMessage('IP2 no pertenece a misma subred clase B');
  // y, en este caso, es claro que también pertenece a clase A
end;

Saludos
__________________
Daniel Didriksen

Guía de estilo - Uso de las etiquetas - La otra guía de estilo ....
Responder Con Cita