[107147] 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 (Andree Toonk)
Wed Aug 27 11:50:49 2008

Date: Wed, 27 Aug 2008 17:50:44 +0200
From: Andree Toonk <andree+nanog@toonk.nl>
To: nanog@nanog.org
In-Reply-To: <48B55EED.4070700@karnaugh.za.net>
Errors-To: nanog-bounces@nanog.org

.-- 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/



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