Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unpacking hex string into array

390 views
Skip to first unread message

Dan Peterson

unread,
Jun 10, 1998, 3:00:00 AM6/10/98
to

How can I unpack a hex string into an array of words?

For example, given the following hex string:

$str = '0000016500000002';
@data = unpack("H8 H8", $str);

@data should contain:

(357, 2)

Obviously, the unpack command above doesn't work, or I'd just use that.
I
included it to show how I want to split the hex string.

Here's another example:

$str = '0000015d001c00110000002d00400000006d000000f0';
@data = unpack("H8 H4 H4 H8 H4 H8 H8", $str);

and @data should contain:

(349, 28, 17, 45, 64, 109, 240)

The hex strings I'm processing are actually over 700 bytes long, and
contain
around 20 values.

Thanks for any help.

PS. Please respond directly to my email address, or at least CC me.

--
------------------------------------------------------------------------
Dan Peterson System Architect SPRYNET
Email: pe...@spry.com Phone: 425-957-8240 FAX:
425-957-6240
------------------------------------------------------------------------


Tom Phoenix

unread,
Jun 11, 1998, 3:00:00 AM6/11/98
to

On Wed, 10 Jun 1998, Dan Peterson wrote:

> For example, given the following hex string:
>
> $str = '0000016500000002';
> @data = unpack("H8 H8", $str);
>
> @data should contain:
>
> (357, 2)

Who says? :-)

But it looks to me as if you're wanting first to pack those hex values to
make a 32-bit-long string, then unpack _that_ as an integer. Could that be
what you want?

> The hex strings I'm processing are actually over 700 bytes long, and
> contain around 20 values.

So, each value is around 35 bytes long? I don't know that you're going to
be able to turn a 35-byte value into a Perl number on most modern
machinery without using something like Math::Big*. Hope this helps!

--
Tom Phoenix Perl Training and Hacking Esperanto
Randal Schwartz Case: http://www.rahul.net/jeffrey/ovs/


Jack Chastain

unread,
Jun 15, 1998, 3:00:00 AM6/15/98
to Dan Peterson

I have recently had some fun working with binary strings in hex format. I
believe you are missing the problem.

A string which contains the characters '0000016500000002' contains a total
of 32 Hex characters - not 16 as your code indicates ('H8 H8'). The
characters stored (you can see this by adding the line print "@data\n"; to
your example) will be 30303030 30313635. This is correct, as a zero is "30"
in hex representation - two hexadecimal values.

If you want to read the hex representation of an 8-character string (a
REALLY poor way to store data!), then you should probably read the data as
A8, then do something like this:

@data = unpack("A8", $str);
print hex @data[0] . "\n";

for the first value ('0000165'), this returns 357, which is the decimal
value of the hex 165. If, on the other hand, 165 is the decimal value for
which you want the hex (A5) value, then you may have to use sprintf as
documented in teh camel book under the description of 'hex'.

Finally, one thing that should have been immediately obvious, but missed my
attention, was in using H4 - for some reason, I expected H4 to read 4 bits,
then another H4 to read another four. Silly me - the computer cannot discern
less than a single byte, so I obviously moved on to the next byte and
grabbed the first four bits there!

The key to remember: H (and h) read BITS - not desired characters. Using od
is sometimes helpful, though I use a file editor to help me along.

Best of luck.

Jack Chastain.

Dan Peterson wrote:

> How can I unpack a hex string into an array of words?
>

> For example, given the following hex string:
>
> $str = '0000016500000002';
> @data = unpack("H8 H8", $str);
>
> @data should contain:
>
> (357, 2)
>

> Obviously, the unpack command above doesn't work, or I'd just use that.
> I
> included it to show how I want to split the hex string.
>
> Here's another example:
>
> $str = '0000015d001c00110000002d00400000006d000000f0';
> @data = unpack("H8 H4 H4 H8 H4 H8 H8", $str);
>
> and @data should contain:
>
> (349, 28, 17, 45, 64, 109, 240)
>

> The hex strings I'm processing are actually over 700 bytes long, and
> contain
> around 20 values.
>

0 new messages