I was doing a network linux install and I was booting via tftp. My configuration files were using hexidecimal versions of IP addresses. So I wrote this to translate ip addresses to hex:
#!/usr/bin/perl -w use strict; my $first; my $ip; # use the address from @ARGV if it exists or request from STDIN if (@ARGV) { $ip = shift @ARGV; } else { print "Enter an ip address: "; chomp( my $ip = <STDIN> ); } # split the ip address into subnets and store in an array my @subnets = split(/\./, $ip); # ensure the ip address is valid. sanitize(@subnets); for (0..$#subnets) { # take each subnet and generate the hex out of the subnet divided +by 16 # then the remainder my $pair = hexize((int($subnets[$_]/16)),($subnets[$_]%16)); print $pair; } print "\n"; sub sanitize { # takes a subnet and makes sure that its not over 255, # greater than 0 and that there are 4 subnets. foreach $_ (@_) { if ($_ > 255) { print "illegal ip address\n"; exit; } elsif ($_ < 0) { print "illegal ip address\n"; exit; } elsif ($#_ != 3) { print "illegal ip address\n"; exit; } } } sub hexize { # Takes two values # 1: the subnet divided by 16 # 2: the subnet divided by 16's remainder # Then it applies the right letter if applicable my @unit; foreach $_ (@_) { if ($_ == 10) { push @unit,"A"; } elsif ($_ == 11) { push @unit,"B"; } elsif ($_ == 12) { push @unit,"C"; } elsif ($_ == 13) { push @unit,"D"; } elsif ($_ == 14) { push @unit,"E"; } elsif ($_ == 15) { push @unit,"F"; } else { push @unit,$_; } } return "$unit[0]$unit[1]"; }

enjoy!

-Actualize

In reply to IP to hex translator by actualize

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.