nanog mailing list archives

Re: interger to I P address


From: bfeeny () mac com
Date: Wed, 27 Aug 2008 16:47:13 +0000

The NANOG way: www.google.com


------Original Message------
From: Boyd, Benjamin R
To: Andree Toonk
To: nanog () nanog org
Sent: Aug 27, 2008 12:38 PM
Subject: RE: interger to I P address

The harder way:

Decimal: 1089055123
Hex (dashes inserted at octals): 40-E9-A9-93 Decimal (of 
each octet): 
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 = shift;
   my $four = $one & 0xff;
   $one >>= 8;
   my $three = $one & 0xff;
   $one >>= 8;
   my $two = $one & 0xff;
   $one >>= 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) 
{
  $max_value = pow(2,32);           //4,294,967,296 
  $bug_fix = 0;
  settype($integer, float);

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

***************************************************************************************

The information contained in this message, including attachments, may contain 
privileged or confidential information that is intended to be delivered only to the 
person identified above. If you are not the intended recipient, or the person 
responsible for delivering this message to the intended recipient, Windstream requests 
that you immediately notify the sender and asks that you do not read the message or its 
attachments, and that you delete them without copying or sending them to anyone else.




Sent from my Verizon Wireless BlackBerry

Current thread: