문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top