2004. szeptember 21., kedd

Convert a normal IP Address to a DWord IP Address


Problem/Question/Abstract:

We sometimes link to a URL like "http://3232235778". This notation is known as DWord IP Address. How can we convert a regular IP Address to a DWord IP Address.

Answer:

Solve 1:

The following Function may not be the most elegante one, but it works. The function will convert an IP Address passed as a string, and returns a string with the converted DWord value. You can test the result with the "Ping" command.

NOTE: you must add "Math" to "Uses" for the "IntPower" Function;

******************************************************************
          This code is FREE. It was compiled on Delphi 3.
******************************************************************

function IP2HEX(OrgIP: string): string;
var
  OrgVal: string; // Saved Original IP Address
  O1, O2, O3, O4: string; // Original IP Split
  H1, H2, H3, H4: string; // Octet To Hex
  HexIP: string; // All Hex Strings United
  XN: array[1..8] of Extended;
  Flt1: Extended;
  Xc: Integer;
begin

  // Save in reverse order for easy "Case"
  Xn[8] := IntPower(16, 0);
  Xn[7] := IntPower(16, 1);
  Xn[6] := IntPower(16, 2);
  Xn[5] := IntPower(16, 3);
  Xn[4] := IntPower(16, 4);
  Xn[3] := IntPower(16, 5);
  Xn[2] := IntPower(16, 6);
  Xn[1] := IntPower(16, 7);

  // Save Original IP Address
  OrgVal := OrgIP;
  O1 := Copy(OrgVal, 1, Pos('.', OrgVal) - 1);
  Delete(OrgVal, 1, Pos('.', OrgVal));
  O2 := Copy(OrgVal, 1, Pos('.', OrgVal) - 1);
  Delete(OrgVal, 1, Pos('.', OrgVal));
  O3 := Copy(OrgVal, 1, Pos('.', OrgVal) - 1);
  Delete(OrgVal, 1, Pos('.', OrgVal));
  O4 := OrgVal;

  H1 := IntToHex(StrToInt(O1), 2);
  H2 := IntToHex(StrToInt(O2), 2);
  H3 := IntToHex(StrToInt(O3), 2);
  H4 := IntToHex(StrToInt(O4), 2);

  // Here we have the HEX value of IP Address
  HexIP := H1 + H2 + H3 + H4;

  // Start Convert Huge HEX to Float variable
  Flt1 := 0;
  for Xc := 1 to 8 do
  begin
    case HexIP[Xc] of
      '0'..'9': Flt1 := Flt1 + (StrToInt(HexIP[XC]) * Xn[Xc]);
      'A': Flt1 := Flt1 + (10 * Xn[Xc]);
      'B': Flt1 := Flt1 + (11 * Xn[Xc]);
      'C': Flt1 := Flt1 + (12 * Xn[Xc]);
      'D': Flt1 := Flt1 + (13 * Xn[Xc]);
      'E': Flt1 := Flt1 + (14 * Xn[Xc]);
      'F': Flt1 := Flt1 + (15 * Xn[Xc]);
    end;
  end;
  Result := FloatToStr(Flt1);
end;


Solve 2:

function IpStringToLong(Ip: string): longword;
var
  i, Shift: integer;
  Temp: string;
begin
  Temp := '';
  Shift := 24;
  Result := 0;
  for i := 1 to Length(Ip) do
  begin
    if Ip[i] = '.' then
    begin
      try
        Result := Result or (byte(StrToInt(Temp)) shl Shift);
      finally
        Temp := '';
        Dec(Shift, 8);
      end;
    end
    else
      Temp := Temp + Ip[i];
  end;
  if Shift <> 0 then
    Result := 0;
end;


Solve 3:

function IP2Number(IP: string): dword;
var
  I, DotPosition: integer;
  IPWord: dword;
begin
  Result := 0;
  for I := 0 to 3 do
  begin
    DotPosition := Pos('.', IP);
    if (DotPosition = 0) then
    begin
      DotPosition := Length(IP) + 1;
    end; {if}
    IPWord := StrToInt(Copy(IP, 1, DotPosition - 1));
    Result := Result or (IPWord shl ((3 - I) * 8));
    IP := Copy(IP, DotPosition + 1, Length(IP));
  end; {for}
end;


Solve 4:

function IpToWord(pIP: PChar): longword;
var
  Block: integer;
begin
  Result := 0;
  Block := 0;
  repeat
    case pIP^ of
      #00, '.':
        begin
          Result := Result shl 8 + Block;
          Block := 0;
        end;
      '0'..'9': Block := Block * 10 + Ord(pIp^) - 48;
      ' ': { allow spaces }
    else
      raise {some error }
    end;
    Inc(pIP);
  until (pIP - 1)^ = #00;
end;

call it using

x := IpToWord(PChar(mySTring))

Nincsenek megjegyzések:

Megjegyzés küldése