unpack()ing fractions of bytes 
Author Message
 unpack()ing fractions of bytes

I have need to extract two 4 bit values from a byte (along with a bunch
o' other fields in a binary structure).

Is there any way of doing this directly with unpack()?

I had expected to be able to say:

($h, $l) = unpack('hh', $d);

but each 'h' seems to eat a whole byte, ditto for 'b'.

map { hex } unpack("HXh", $d) does what I want, though I don't view
that solution as particularly elegant, nor general (I don't want to
apply hex to the rest of my structure).

In lieu of a direct unpack template, I will extract the containing byte
and mask off and shift to get each part...

Regards,

Ben Low

_____________________________________________________________________________
http://www.*-*-*.com/ - Yahoo! Movies
- Now showing: Dude Where's My Car, The Wedding Planner, Traffic..



Fri, 03 Oct 2003 13:36:29 GMT  
 unpack()ing fractions of bytes

Quote:

> I have need to extract two 4 bit values from a byte (along with a bunch
> o' other fields in a binary structure).

For myself I would probably unpack a byte then shift or mask as appropriate
to get the 4 bit values.

/J\
--
Jonathan Stowe                      |
<http://www.gellyfish.com>          |      This space for rent
                                    |



Fri, 03 Oct 2003 18:59:26 GMT  
 unpack()ing fractions of bytes

Quote:

> For myself I would probably unpack a byte then shift or mask as appropriate
> to get the 4 bit values.

I'd probably use vec() to do the shifting and masking.

-- HansM



Sat, 04 Oct 2003 03:04:37 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Query Record Selection display in Another Form

2. seeking easier pack/unpack from integer to 4-byte hex

3. help: dup'ing/tee'ing

4. convert network byte order to host byte order

5. GET, but starting at byte 5000, and just get 2000 byte

6. bytes read != bytes written?

7. (Q) Reading a binary file byte-by-byte

8. Delphi vs. the other options

9. Record/Key Deleted... what and why?

10. Password protect table - Delphi 1

11. Tracing program execution, & logging to file

12. solution 2000......year 2000 crisis

 

 
Powered by phpBB® Forum Software