471,357 Members | 1,274 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,357 software developers and data experts.

struct.unpack less than 1 byte

hello all,

i need to read from a file a struct like this [1byte, 12bits, 12bits]
reading 1 byte or more is not a problem ... but the 12 bits values
are ...

thanks

Oct 10 '07 #1
4 3328
2007/10/10, cprogrammer <cp*********@gmail.com>:
hello all,

i need to read from a file a struct like this [1byte, 12bits, 12bits]
reading 1 byte or more is not a problem ... but the 12 bits values
are ...

thanks

--
http://mail.python.org/mailman/listinfo/python-list
12bits, 12bits == 3 byes

--
-- Guilherme H. Polo Goncalves
Oct 10 '07 #2
You are able to read single bits from file in C ?

You'll have to read the bytes and than perform some bitwise operations on
them to extract the bits
hello all,

i need to read from a file a struct like this [1byte, 12bits, 12bits]
reading 1 byte or more is not a problem ... but the 12 bits values
are ...

thanks
Oct 10 '07 #3
On Oct 10, 8:15 pm, "Guilherme Polo" <ggp...@gmail.comwrote:
2007/10/10, cprogrammer <cprogram...@gmail.com>:
i need to read from a file a struct like this [1byte, 12bits, 12bits]
reading 1 byte or more is not a problem ... but the 12 bits values
are ...

12bits, 12bits == 3 byes
and 8 + 12 + 12 = 32 :-)

Assuming little-endianess and that all 3 items are unsigned:
>>import struct
bytes = "\x21\x43\xba\xdc"
i32 = struct.unpack("<I", bytes)[0]
hex(i32)
'0xdcba4321L'
>>fields = map(int, (i32 & 0xff, (i32 >8) & 0xfff, i32 >20))
fields
[33, 2627, 3531]
>>map(hex, fields)
['0x21', '0xa43', '0xdcb']
>>>
Oct 10 '07 #4
On Wed, 10 Oct 2007 03:42:15 -0700, John Machin wrote:
On Oct 10, 8:15 pm, "Guilherme Polo" <ggp...@gmail.comwrote:
>2007/10/10, cprogrammer <cprogram...@gmail.com>:
i need to read from a file a struct like this [1byte, 12bits, 12bits]
reading 1 byte or more is not a problem ... but the 12 bits values
are ...

12bits, 12bits == 3 byes

and 8 + 12 + 12 = 32 :-)
[snipped bitfiddling]

Or, if you're doing more of those parsing tasks (or just dislike bit
shifting), you could have a look into `construct`_::
>>from construct import *
foo = BitStruct("foo",
... Octet("spam"), # synonym for BitField("spam", 8)
... BitField("ham", 12),
... BitField("eggs", 12)
... )
>>foo
Buffered('foo')
>>foo.parse("\xff\xff\xff\xff")
Container(eggs = 4095, ham = 4095, spam = 255)

Cheers,
Stargaming

... _construct: http://construct.wikispaces.com/
Oct 10 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by R. Rajesh Jeba Anbiah | last post: by
2 posts views Thread by Angelo Secchi | last post: by
reply views Thread by Josiah Carlson | last post: by
5 posts views Thread by grant | last post: by
2 posts views Thread by Sergey Dorofeev | last post: by
10 posts views Thread by Giovanni Bajo | last post: by
11 posts views Thread by nephish | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.