Abolish domain-names! Forget ip-addresses! Use easy-to-remember decimal numbers!

It's not exactly complicated code, but a lot of people don't know you can also use decimals instead of ip-addresses: for 206.170.14.75, convert each element into its binary equivalent, join the four strings and convert the resulting string into decimal (which will be a number less than 2^32).
#!/usr/bin/perl -w use strict; use Socket; for my $host (@ARGV) { my $addr = inet_ntoa( scalar gethostbyname($host) ); my $bin; $bin .= dec2bin($_) for (split /[.]/, $addr); print bin2dec($bin), "\n"; } sub bin2dec { unpack("N", pack("B32", substr("0" x 32 . shift, -32))); } sub dec2bin { my $str = unpack("B32", pack("N", shift)); # we only need the lowest 8 bits (ipv4 range 0-255) return substr $str, length($str) - 8; }

Example:
[ar0n@zendo perl]% perl ip2dec.pl perlmonks.org 3467251275
Now load up your browser and be amazed! (your perlmonks cookie will, of course, not work)

Replies are listed 'Best First'.
RE: Convert hostname to decimal form
by japhy (Canon) on Oct 22, 2000 at 05:01 UTC
    You can do away with Socket and that binary conversion by just using the 4-byte raw data:
    #!/usr/bin/perl use strict; for my $host (@ARGV) { my $addr = gethostbyname($host); my $sum = 0; $sum = $sum * 256 + ord for split //, $addr; print "$host: $sum\n"; }


    $_="goto+F.print+chop;\n=yhpaj";F1:eval
Re: Convert hostname to decimal form
by Anonymous Monk on Nov 24, 2000 at 18:36 UTC
    print unpack('N', scalar gethostbyname($host)), "\n";
Re: Convert hostname to decimal form
by bittondb (Novice) on Oct 08, 2001 at 06:57 UTC
    Decimal representations of IP addresses also lends for fast DB sorts, since the field datatype is UNSIGNED INT, instead of VARCHAR(15).