My Perl program:
say 'Hexadecimal notation in a Perl script';
my$str = pack 'H*', '987456123';
print unpack('H*', $str), "\n";
print unpack('h*', $str), "\n";
Output:
Hexadecimal notation in a Perl script
1. 9874561230 # Why is it showing zero here?
2. 8947652103
In my 1
result why is it showing zero? What is cause of this?
You have odd number of chars in 987456123
and H*
packing requires even, so it assumes zero for the last pair (98
, 74
, 56
, 12
, and 30
at the end).
From perldoc:
Starting from the beginning of the template to pack(), each pair of characters is converted to 1 character of output. [...] If the length of the input string is not even, it behaves as if padded by a null character at the end. Similarly, "extra" nybbles are ignored during unpacking.