Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

IP to hex translator

by actualize (Monk)
on Aug 14, 2008 at 06:33 UTC ( [id://704295]=CUFP: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
Re: IP to hex translator
by Fletch (Bishop) on Aug 14, 2008 at 12:28 UTC

    Fore.

    $ perl -MSocket -e 'printf(("%02x"x4)."\n",unpack"C4",inet_aton shift) +' 127.0.0.1 7f000001

    Update: Granted there's no error checking as the OP's, but that's a single regex at worst (and is left as an exercise for the reader . . . :).

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

      ... and you don't even have to write the pattern yourself.

      use Regexp::Common 'net'; $ip =~ /^$RE{net}{IPv4}\z/ or die;

      lodin

Re: IP to hex translator
by Anonymous Monk on Aug 14, 2008 at 06:43 UTC
    Fixed it for you :)
    #!/usr/bin/perl -- use strict; use warnings; use Net::IP; my $ip; # only my, only $ip if (@ARGV) { $ip = shift @ARGV; } else { print "Enter an ip address: "; chomp( $ip = <STDIN> ); } $ip = new Net::IP ( $ip ) or die (Net::IP::Error()); print $ip->hexip(), "\n";
Re: IP to hex translator
by alexm (Chaplain) on Aug 14, 2008 at 11:29 UTC
    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";

    sprintf makes it easier:

    print hexize(@subnets), "\n"; sub hexize { return sprintf("%02x%02x%02x%02x", @_); }

    Obviously, Net::IP makes things much easier, as anonymonk pointed out.

Re: IP to hex translator
by actualize (Monk) on Aug 14, 2008 at 16:08 UTC

    Looks like I have a lot to learn. I'll be digging through perlfunc this weekend.

    -Actualize
Re: IP to hex translator
by Oleg_T (Initiate) on Sep 18, 2009 at 23:46 UTC
    perl -e "printf '%02x' x 4, split '\.', '127.0.0.1'"
Re: IP to hex translator
by Oleg_T (Initiate) on Sep 18, 2009 at 23:00 UTC
    perl -e "map {printf '%02x'} split '\.', '127.0.0.1'"
      sorry, perl -e "map {printf '%02x', $_} split '\.', '127.0.0.1'"
      perl -e "print unpack('H*', pack('C*', split ('\.', '127.0.0.1')))"
      Or (whithout brackets)
      perl -e "print unpack 'H*', pack 'C*', split '\.', '127.0.0.1'"

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://704295]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-26 05:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found