in reply to left justify/pad hex value

Use %02x instead of %x

If someone has a better suggestion on how to accomplish this

unpack 'H*', pack 'C4', @octets;

Replies are listed 'Best First'.
Re^2: left justify/pad hex value
by TheBigAmbulance (Acolyte) on Mar 03, 2010 at 19:36 UTC
    one other question. Let's say LIST.file contained:
    10.34.2.252 10.34.2.250 10.34.2.248
    I understand how to open the file. But I need a way to read each octet and place it accordingly into $octet1, etc.
      $ echo "10.34.2.252 > 10.34.2.250 > 10.34.2.248" | perl -e' use Socket; while ( <> ) { chomp; print "$_ -> ", unpack( "H*", inet_aton $_ ), "\n"; } ' 10.34.2.252 -> 0a2202fc 10.34.2.250 -> 0a2202fa 10.34.2.248 -> 0a2202f8

        Or just

        $ echo "10.34.2.252 > 10.34.2.250 > 10.34.2.248" | perl -nle 'print "$_ -> ",unpack "H*", eval' 10.34.2.252 -> 0a2202fc 10.34.2.250 -> 0a2202fa 10.34.2.248 -> 0a2202f8

        ;-)

        Just found out this works as well:
        #!/usr/bin/perl use strict; use warnings; my $data = '10.34.2.252'; my @values = split('\.', $data); foreach my $val (@values) { $val = sprintf("%02x", $val); print $val; } print "\n"; exit 0;
        Thanks for the help!