Why doesn't my package () pack my data?

What I'm trying to do is simple. Here below.

my @arr = split(/\s+/,"50 00 9F 11 00 28 82 48 21 84 BC 00 01 02 01 00 09 01 38 00 23 05 08 01 01 02 00 00 18 00 50 05 00 00 00 00 00 00 00 00 02 00 0C FE CE 00 0F 00 FD FF 2D 00 00 00 00 00 04 01 0C FE"); my @hexData; my $i=0; foreach my $elem(@arr){ $hexData[$i]=hex($elem); $i++; } my $data= pack ('C', @hexData); print $data; 

And its not working :( Could you help?

+1
source share
2 answers

The TLP solution is perfectly correct, but pack really has the ability to deal with hex.

 my $data = "50 00 9F 11 00 28 82 48 21 84 BC 00 01 02 01 00 09 01 38 00 23 05 08 01 01 02 00 00 18 00 50 05 00 00 00 00 00 00 00 00 02 00 0C FE CE 00 0F 00 FD FF 2D 00 00 00 00 00 04 01 0C FE"; $data =~ tr/ //d; # Remove the spaces print pack "H*", $data; 

does everything without an intermediate array.

+8
source

I am not very familiar with the pack function, but it seems to me that your template expects only one value.

Maybe you should try

 my $data = pack ('C*', @hexData); 

And while you're on it, upgrade your code to something more:

 my @arr = qw(50 00 9F 11 00 28 82 48 21 84 BC 00 01 02 01 00 09 01 38 00 23 05 08 01 01 02 00 00 18 00 50 05 00 00 00 00 00 00 00 00 02 00 0C FE CE 00 0F 00 FD FF 2D 00 00 00 00 00 04 01 0C FE); my @hexData; foreach my $elem (@arr) { push @hexData, hex($elem); } my $data = pack ('C*', @hexData); print $data; 

Or even:

 my $data = pack("C*", map(hex, @arr)); 
+3
source

Source: https://habr.com/ru/post/1439070/


All Articles