h A hex string (low nybble first).
H A hex string (high nybble first).
...
* The "h" and "H" fields pack a string that many nybbles
(4-bit groups, representable as hexadecimal digits,
0-9a-f) long.
Each byte of the input field of pack() generates 4 bits
of the result. For non-alphabetical bytes the result is
based on the 4 least-significant bits of the input byte,
i.e., on "ord($byte)%16". In particular, bytes "0" and
"1" generate nybbles 0 and 1, as do bytes "\0" and "\1".
For bytes "a".."f" and "A".."F" the result is compatible
with the usual hexadecimal digits, so that "a" and "A"
both generate the nybble "0xa==10". The result for bytes
"g".."z" and "G".."Z" is not well-defined.
Starting from the beginning of the input string of
pack(), each pair of bytes is converted to 1 byte of
output. With format "h" the first byte of the pair
determines the least-significant nybble of the output
byte, and with format "H" it determines the
most-significant nybble.
If the length of the input string is not even, it
behaves as if padded by a null byte at the end.
Similarly, during unpack()ing the "extra" nybbles are
ignored.
If the input string of pack() is longer than needed,
extra bytes are ignored. A "*" for the repeat count of
pack() means to use all the bytes of the input field. On
unpack()ing the bits are converted to a string of
hexadecimal digits.
####
my @hex = $hexed =~ /([0-9a-zA-Z]+)/g;
for (@hex){
... pack 'H*', $_;
}
####
C:\>echo abcd1234 >test
C:\>more test
abcd1234
C:\>od -tx1 test
0000000 61 62 63 64 31 32 33 34 20 0d 0a
0000013
C:\>hexdump test
00000000: 61 62 63 64 31 32 33 34 - 20 0D 0A |abcd1234 |
0000000b;
C:\>perl -e"print pack q,H*,, $_ for @ARGV" 61 62 63 64 31 32 33 34
abcd1234