I know this should be simple, but I am having a mental block for the last half hour. I need to take a variable length ascii string and convert it to the appropriate number of bytes. As an example, the string "0x0102" should become two bytes where the MSB byte is 0x01 and LSB byte is 0x02. I know this should be doable with a pack() call, but I can't seem to get it. Here's what I have so far:
which produces the right number of bytes, but with the wrong value of "001000 ...".use strict; use warnings; my $string = "0x010203"; my @in = split(//, $string); shift @in; shift @in; my $bytes = int(scalar(@in) / 2); my $output = pack('H2' x $bytes, @in); # print out the converted output, just to double check print unpack ('H2' x $bytes, $output); $output =~ s/[\x00-\x1F\xFF]/./g; # ASCII representation of hex value +of byte print " $output\n"; # ASCII character for each byte
Thanks for the help!
In reply to converting ASCII hex into binary by gri6507
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |