Discussion:
Convert Integer value to IP Address
(too old to reply)
Vikrant
2005-07-21 13:14:57 UTC
Permalink
Hi

Please tell me how do i convert a long integer IP address to dotted quad
IP address using perl language.

For example:- If IP address is 167772260 then how do i convert it into
dotted quad IP address.

I am able to do it by applying some mathematical operations, but i am
looking for a function in perl the can do the same,because in my code i
have deal with many IP addresses.

or plz suggest me any other way to do it.


With Regards
Vikrant
Ian Wilson
2005-07-21 13:27:02 UTC
Permalink
Post by Vikrant
Hi
Please tell me how do i convert a long integer IP address to dotted quad
IP address using perl language.
For example:- If IP address is 167772260 then how do i convert it into
dotted quad IP address.
I am able to do it by applying some mathematical operations, but i am
looking for a function in perl the can do the same,because in my code i
have deal with many IP addresses.
or plz suggest me any other way to do it.
perldoc -q "IP address"
Paul Lalli
2005-07-21 14:17:45 UTC
Permalink
Post by Vikrant
Please tell me how do i convert a long integer IP address to dotted quad
IP address using perl language.
For example:- If IP address is 167772260 then how do i convert it into
dotted quad IP address.
Relatively sure you want Socket.pm's inet_ntoa() function. Have a look
at it in
perldoc Socket

Paul Lalli
a***@white-eagle.invalid.uk
2005-07-21 14:49:18 UTC
Permalink
Post by Paul Lalli
Post by Vikrant
Please tell me how do i convert a long integer IP address to dotted quad
IP address using perl language.
For example:- If IP address is 167772260 then how do i convert it into
dotted quad IP address.
Relatively sure you want Socket.pm's inet_ntoa() function. Have a look
at it in
perldoc Socket
Yes... although it needs some juggling:

print inet_ntoa(inet_aton(167772260));

Yields the required '10.0.0.100'.

Axel
Richard Gration
2005-07-21 15:19:35 UTC
Permalink
Post by a***@white-eagle.invalid.uk
Post by Paul Lalli
Post by Vikrant
For example:- If IP address is 167772260 then how do i convert it into
dotted quad IP address.
Relatively sure you want Socket.pm's inet_ntoa() function. Have a look
at it in
perldoc Socket
print inet_ntoa(inet_aton(167772260));
Yields the required '10.0.0.100'
This example using pack also yields the correct answer

perl -MSocket -e 'print inet_ntoa(pack 'N',167772260)'

but I'm not sure if this is a legit use of pack.

I find pack/unpack utterly confusing. Which is to say, whenever I try to
use it, I think really hard about what I'm trying to do, I read and
re-read "perldoc -f pack", I concentrate, very carefully write my line of
code thinking hard along the way and then: Voila! A complete load of tripe
comes out :-(

Rich
J. Gleixner
2005-07-21 16:51:19 UTC
Permalink
Post by Vikrant
Hi
Please tell me how do i convert a long integer IP address to dotted quad
IP address using perl language.
For example:- If IP address is 167772260 then how do i convert it into
dotted quad IP address.
I am able to do it by applying some mathematical operations, but i am
looking for a function in perl the can do the same,because in my code i
have deal with many IP addresses.
or plz suggest me any other way to do it.
Net::Netmask (http://search.cpan.org/~muir/Net-Netmask-1.9012/) contains
a lot of useful methods pertaining to IP addresses and CIDR notation.

sub int2quad
{
return join('.',unpack('C4', pack("N", $_[0])));
}
Christian Winter
2005-07-22 05:28:59 UTC
Permalink
Post by J. Gleixner
sub int2quad
{
return join('.',unpack('C4', pack("N", $_[0])));
}
Which is as far as i could test the fastest solution, at
least on x86 with perl 5.8.

-Chris
Vikrant
2005-07-23 06:26:42 UTC
Permalink
Thanks to all for the information.

vikrant

Continue reading on narkive:
Loading...