473,383 Members | 1,818 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,383 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 3497
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. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.