473,239 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 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 3484
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: R. Rajesh Jeba Anbiah | last post by:
Recently I've done a project to fiddle WAV (format) files. To read the headers, I've used something like: fseek($fp, 40); //Data length is at offset 40 $data_len = fread($fp, 4); //4 bytes ...
2
by: Angelo Secchi | last post by:
I'm trying to use the unpack method in the struct module to parse a binary file without success. I have a binary file with records that include many fields for a total length of 1970. Few days ago...
0
by: Josiah Carlson | last post by:
Good day everyone, I have produced a patch against the latest CVS to add support for two new formatting characters in the struct module. It is currently an RFE, which I include a link to at the...
5
by: Geoffrey | last post by:
Hope someone can help. I am trying to read data from a file binary file and then unpack the data into python variables. Some of the data is store like this; xbuffer:...
5
by: grant | last post by:
Hi All, I am pretty new to python and am having a problem intepreting binary data using struct.unpack. I am reading a file containing binary packed data using open with "rb". All the values are...
2
by: Sergey Dorofeev | last post by:
I can use string.unpack if string in struct uses fixed amount of bytes. But is there some extension to struct modue, which allows to unpack zero-terminated string, size of which is unknown? E.g....
16
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
10
by: Giovanni Bajo | last post by:
Hello, given the ongoing work on struct (which I thought was a dead module), I was wondering if it would be possible to add an API to register custom parsing codes for struct. Whenever I use it...
11
by: nephish | last post by:
hello there, all. i have a difficult app that connects to a server to get information for our database here. this server is our access point to some equipment in the field that we monitor. ...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.