I have a hex string (length 48 chars) that I want to convert to raw bytes with the pack function in order to put it in a Win32 vector of bytes.
How I can do this with Perl?
I have a hex string (length 48 chars) that I want to convert to raw bytes with the pack function in order to put it in a Win32 vector of bytes.
How I can do this with Perl?
The steps are:
For example:
use strict;
use warnings;
my $string = 'AA55FF0102040810204080';
my @hex = ($string =~ /(..)/g);
my @dec = map { hex($_) } @hex;
my @bytes = map { pack('C', $_) } @dec;
Or, expressed more compactly:
use strict;
use warnings;
my $string = 'AA55FF0102040810204080';
my @bytes = map { pack('C', hex($_)) } ($string =~ /(..)/g);
my $bytes = pack "H*", $hex;
See perlpacktut for more information.