in reply to Dotless IP address problem

Just following the formulas on the web page:
sub ip_to_dotless { return 0+ eval " 0x" . sprintf("%02x%02x%02x%02x", split(/\./, + shift)); } sub dotless_to_ip { my $hex=reverse sprintf("%x", shift); my @ip; for(0..3) { push(@ip, 0+ eval "0x" . reverse substr($hex, 0, 2, "" +)); } join('.', reverse @ip); }
This seems to work nicely.

Replies are listed 'Best First'.
Re: Re: Dotless IP address problem
by guha (Priest) on Oct 19, 2001 at 00:46 UTC
    A slight twist which works for your two examples anyway.
    sub dotless_to_ip { #use integer; my $dotless = shift; my $index = 3; my @ip = (); while ( @ip < 4 ) { my $temp = 256 ** $index--; push @ip, int($dotless / $temp) ; $dotless -= $ip[ -1 ] * $temp; } return join '.', @ip; }
    I guess one could attack the problem using binary shifts (>>) and bitwise and's as well.