[107153] in North American Network Operators' Group

home help back first fref pref prev next nref lref last post

RE: interger to I P address

daemon@ATHENA.MIT.EDU (Boyd, Benjamin R)
Wed Aug 27 12:38:33 2008

Date: Wed, 27 Aug 2008 11:38:27 -0500
From: "Boyd, Benjamin R" <Benjamin.R.Boyd@windstream.com>
To: "Andree Toonk" <andree+nanog@toonk.nl>, <nanog@nanog.org>
Errors-To: nanog-bounces@nanog.org

>>> The harder way:
>>>
>>> Decimal: 1089055123
>>> Hex (dashes inserted at octals): 40-E9-A9-93 Decimal (of=20
>each octet):=20
>>> 64-233-169-147 IP Address: 64.233.169.147
>>
>> The Python way
>>
>> >>> import socket, struct
>> >>> socket.inet_ntoa(struct.pack('>l', 1089055123))
>> '64.233.169.147'
>
>The Perl way:
>
>sub ntoa
>{
>    my $one =3D shift;
>    my $four =3D $one & 0xff;
>    $one >>=3D 8;
>    my $three =3D $one & 0xff;
>    $one >>=3D 8;
>    my $two =3D $one & 0xff;
>    $one >>=3D 8;
>    return "$one.$two.$three.$four";
>}
>
>#or in one line, like ipcalc does:
>sub ntoa_in_one_line { join(".", unpack("CCCC", pack("N", $_[0]))); }
>
>print ntoa(1089055123) . "\n";
>print ntoa_in_one_line(1089055123) . "\n";
>
>

The PHP way:
function convertIntegerToIpv4($integer)=20
{
  $max_value =3D pow(2,32);           //4,294,967,296=20
  $bug_fix =3D 0;
  settype($integer, float);

  if($integer > 2147483647) $bug_fix =3D 16777216;
=20=20=20=20
  if(is_numeric($integer))=20
  {=20
    if ($integer >=3D $max_value || $integer < 0)
    {
      return ('Not a valid IPv4 integer');
    }
    $ip =3D (sprintf("%u.%u.%u.%u",
            $integer / 16777216,
            (($integer % 16777216) + $bug_fix) / 65536,
            (($integer % 65536) + $bug_fix / 256) / 256,
            ($integer % 256) + $bug_fix / 256 / 256
            )
          );=20
    return($ip);
  }
  else {
     return('');
  }
}

***************************************************************************=
************

The information contained in this message, including attachments, may conta=
in=20
privileged or confidential information that is intended to be delivered onl=
y to the=20
person identified above. If you are not the intended recipient, or the pers=
on=20
responsible for delivering this message to the intended recipient, Windstre=
am requests=20
that you immediately notify the sender and asks that you do not read the me=
ssage or its=20
attachments, and that you delete them without copying or sending them to an=
yone else.



home help back first fref pref prev next nref lref last post