gri6507 has asked for the wisdom of the Perl Monks concerning the following question:
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!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: converting ASCII hex into binary
by BrowserUk (Patriarch) on Apr 28, 2006 at 00:50 UTC | |
|
Re: converting ASCII hex into binary
by davidrw (Prior) on Apr 28, 2006 at 00:17 UTC | |
|
Re: converting ASCII hex into binary
by ikegami (Patriarch) on Apr 28, 2006 at 02:36 UTC |