http://qs321.pair.com?node_id=917006

andreas1234567 has asked for the wisdom of the Perl Monks concerning the following question:

Honorable Monks,

An application reads binary data from an external system, and saves it using $h = unpack("H*", $b). If I try to reconstruct the binary data with $b = pack("H*", $h), the binary data will sometimes have a trailing null byte. Is it possible to make sure the binary data as it were (i.e. never add the null byte)?

A little code sample to illustrate the problem indicates that is works when input length is even only:
$ cat pack.t use strict; use warnings; use Test::More qw /no_plan/; my @arr = (0 .. 9, 'a' .. 'f'); my $hexdata = q{}; while (@arr) { $hexdata .= shift @arr; my $binary_data = pack("H*", $hexdata); cmp_ok(unpack('H*', $binary_data), 'eq', $hexdata); } __END__ $ perl pack.t not ok 1 # Failed test at pack.t line 11. # got: '00' # expected: '0' ok 2 not ok 3 # Failed test at pack.t line 11. # got: '0120' # expected: '012' ok 4 not ok 5 # Failed test at pack.t line 11. # got: '012340' # expected: '01234' ok 6 not ok 7 # Failed test at pack.t line 11. # got: '01234560' # expected: '0123456' ok 8 not ok 9 # Failed test at pack.t line 11. # got: '0123456780' # expected: '012345678' ok 10 not ok 11 # Failed test at pack.t line 11. # got: '0123456789a0' # expected: '0123456789a' ok 12 not ok 13 # Failed test at pack.t line 11. # got: '0123456789abc0' # expected: '0123456789abc' ok 14 not ok 15 # Failed test at pack.t line 11. # got: '0123456789abcde0' # expected: '0123456789abcde' ok 16 1..16 # Looks like you failed 8 tests of 16.
--
No matter how great and destructive your problems may seem now, remember, you've probably only seen the tip of them. [1]