如何使用Perl将48个十六进制字符串转换为字节?

我有一个十六进制字符串(长度为48个字符),我想用 pack

函数转换为原始字节,以便将其放入Win32的字节矢量中.

我怎么能用Perl做到这一点?

步骤是:

>从字符串中提取十六进制字符对.

>将每对转换为十进制数.

>将数字打包为一个字节.

例如:

use strict;
use warnings;

my $string = 'AA55FF0102040810204080';
my @hex    = ($string =~ /(..)/g);
my @dec    = map { hex($_) } @hex;
my @bytes  = map { pack('C', $_) } @dec;

或者,更紧凑地表达:

use strict;
use warnings;

my $string = 'AA55FF0102040810204080';
my @bytes  = map { pack('C', hex($_)) } ($string =~ /(..)/g);

翻译自:https://stackoverflow.com/questions/2427527/how-can-i-convert-a-48-hex-string-to-bytes-using-perl