nanog mailing list archives

Re: interger to I P address


From: Andree Toonk <andree+nanog () toonk nl>
Date: Wed, 27 Aug 2008 17:50:44 +0200

.-- My secret spy satellite informs me that at Wed, 27 Aug 2008, Colin Alston wrote:

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";

Cheers,
 Andree

--
 Andree Toonk
 http://www.toonk.ca/blog/



Current thread: