Reputation: 23
I have some binary data that comes to me from a library function which I store in a variable called: $data
If I hexify this binary data with: $hexstring = unpack "H*", $data;
It performs this hex transformation correctly, I verified my results against the unix commands: od -c and xxd -p :-)
My question is how to I do the reverse operation to pack this data back into a $newdata variable from the hex data ?
if I do a ref($data), it returns a blank result, so I don't understand what format this binary data is originally formated in the $data variable. The library author tells me it is a string, but my experience trying to pack a string, it does not produce the correct result.
I have tried various attempts to "pack" but I don't seem to know how to pack this correctly. I am stumbling on something. :-)
Please help me to understand my packing errors ;-)
Thanks!
Upvotes: 2
Views: 1645
Reputation: 126772
Well it's pretty straightforward in this case. The inverse operation of unpack 'H*'
is pack 'H*'
Here's a program demonstrating
use strict;
use warnings 'all';
use 5.10.1;
my $data = "\x01\x23\x45\x67\x89\xAB\xCD\xEF";
my $hex = unpack "H*", $data;
say $hex;
my $newdata = pack "H*", $hex;
say $newdata eq $data ? 'OK' : 'FAIL';
0123456789abcdef
OK
Upvotes: 3