473,473 Members | 2,169 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

struct.unpack() and bit operations

THANKS FOR THE HELP ON MY LAST INQUIRY! AWESOME ANSWERS.

I am reading in from a binary file data that is formatted as 32 *
16-bit words. So when I read it in I'm apparently not getting or
addressing correctly the upper 16-bits (since I have no choice but
read 32-bit elements - right?).

Is there an easier way to either get 16-bit "unpacks" or grab the
upper 16-bits with success?

Here's what I'm trying:

mask0 = 1111111100000000
mask1 = 0000000011111111

pdr32 = struct.unpack('%si' % (itemsize/4), pdrData.read(itemsize))

while i < 32
pdr16[x] = (pdr32[y] & (mask0 / (2**8)))
pdr16[x+1] = (pdr32[y] & mask1)
x += 2
y += 1
pdrData.seek(32,1)
Thank you!
Mike Spindler
Jul 18 '05 #1
2 2840
mikeSpindler wrote:
THANKS FOR THE HELP ON MY LAST INQUIRY! AWESOME ANSWERS.

I am reading in from a binary file data that is formatted as 32 *
16-bit words. So when I read it in I'm apparently not getting or
addressing correctly the upper 16-bits (since I have no choice but
read 32-bit elements - right?).
You can read any amount of data that you want, including bytes.
Use read(n) where n is the number of bytes to grab.
Is there an easier way to either get 16-bit "unpacks" or grab the
upper 16-bits with success?
Not sure why you're having troubles: there are many ways of unpacking
stuff using struct, and it will handle bytes, words (16-bit values),
and 32-bit values, with the two popular variations on the endianism
of the data handled, and other things.
Here's what I'm trying:

mask0 = 1111111100000000
mask1 = 0000000011111111

pdr32 = struct.unpack('%si' % (itemsize/4), pdrData.read(itemsize))


Does pointing out that there is an "h" specified in addition to "i"
help you? In the docs, "short" refers to a 16-bit value.

-Peter
Jul 18 '05 #2
mikeSpindler wrote:
Is there an easier way to either get 16-bit "unpacks" or grab the
upper 16-bits with success?


You may have a look at the array module:
binary_data = "PleaseDon'tShout"
a = array.array("H", binary_data)
a array('H', [27728, 24933, 25971, 28484, 10094, 21364, 28520, 29813]) a[::2] array('H', [27728, 25971, 10094, 28520]) a[1::2].tostring() 'eaDotSut'


Peter

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
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...
3
by: David Bear | last post by:
I found this simple recipe for converting a dotted quad ip address to a string of a long int. struct.unpack('L',socket.inet_aton(ip)) trouble is when I use this, I get struct.error: unpack...
4
by: OhKyu Yoon | last post by:
Hi! I have a really long binary file that I want to read. The way I am doing it now is: for i in xrange(N): # N is about 10,000,000 time = struct.unpack('=HHHH', infile.read(8)) # do...
2
by: brnstrmrs | last post by:
If I run: testValue = '\x02\x00' junk = struct.unpack('h', testValue) Everything works but If I run testValue = raw_input("Enter Binary Code..:") inputting at the console '\x02\x00' junk...
0
by: Ping Zhao | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I am writing a small program to decode MS bitmap image. When I use statements as follow, it works fine: header = str(struct.unpack('2s',...
2
by: Heikki Toivonen | last post by:
M2Crypto has some old code that gets and sets socket timeouts in http://svn.osafoundation.org/m2crypto/trunk/M2Crypto/SSL/Connection.py, for example: def get_socket_read_timeout(self): return...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.