Perl pack integer

Given this value:

my $value = hex('0x12345678');

      

And I would like mine to be like hexdump

this (same bit order):

0000000 1234 5678

      

I used this method, but it mixes up my value:

open(my $out, '>:raw', 'foo') or die "Unable to open: $!";     
print $out pack('l', $value);  # Test in little endian
print $out pack('l>', $value); # Test in big endian

      

This is what I get:

0000000 5678 1234 3412 7856

      

How can I get the bit ok?

EDIT

So, the problem might come from my hexdump because I am getting the same result with the suggested answer.

$ perl -e 'print pack $_, 0x12345678 for qw( l> N )' | hexdump
0000000 3412 7856 3412 7856

      

I got the correct result with hexdump -C

:

$ perl -e 'print pack $_, 0x12345678 for qw( l> N )' | hexdump -C
00000000  12 34 56 78 12 34 56 78                           |.4Vx.4Vx|

      

And I found an explanation here: hexdump confusion

+3


source to share


1 answer


The option 'l>'

works for me (note there is no call hex

). In addition, N

the following works as a template:



perl -e 'print pack $_, 0x12345678 for qw( l> N )' | xxd
0000000: 1234 5678 1234 5678

      

+3


source







All Articles