473,378 Members | 1,419 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,378 software developers and data experts.

4 byte integer

Greetings everyone. I'm a relative newcomer to python and I have a technical
problem.

I want to split a 32 bit / 4 byte unsigned integer into 4 separate byte
variables according to the following logic: -

bit numbers 0..7 byte 1
bit numbers 8..15 byte 2
bit numbers 16..23 byte 3
bit numbers 24..31 byte 4

Each of these byte variables to contain integer data from 0 to 255 (or 0 to
FF in hex mode)

I had thought that struct.unpack with an input message format of 'I' would
be the way to do it, but its reporting an error that it doesn't want to
accept an integer.

Please can anyone advise?

May 11 '07 #1
3 8785

On May 11, 2007, at 4:25 AM, Paul D Ainsworth wrote:
Greetings everyone. I'm a relative newcomer to python and I have a
technical
problem.

I want to split a 32 bit / 4 byte unsigned integer into 4 separate
byte
variables according to the following logic: -

bit numbers 0..7 byte 1
bit numbers 8..15 byte 2
bit numbers 16..23 byte 3
bit numbers 24..31 byte 4

Each of these byte variables to contain integer data from 0 to 255
(or 0 to
FF in hex mode)

I had thought that struct.unpack with an input message format of
'I' would
be the way to do it, but its reporting an error that it doesn't
want to
accept an integer.

Please can anyone advise?
Have a look at http://aspn.activestate.com/ASPN/Cookbook/Python/
Recipe/113799
May 11 '07 #2
>
Have a look at http://aspn.activestate.com/ASPN/Cookbook/Python/
Recipe/113799
Brilliant - thank you :)
May 11 '07 #3
Paul D Ainsworth <pa*********@nospam.supanet.comwrote:
Greetings everyone. I'm a relative newcomer to python and I have a technical
problem.
I want to split a 32 bit / 4 byte unsigned integer into 4 separate byte
variables according to the following logic: -
bit numbers 0..7 byte 1
bit numbers 8..15 byte 2
bit numbers 16..23 byte 3
bit numbers 24..31 byte 4
Each of these byte variables to contain integer data from 0 to 255 (or 0 to
FF in hex mode)
I had thought that struct.unpack with an input message format of 'I' would
be the way to do it, but its reporting an error that it doesn't want to
accept an integer.
Please can anyone advise?
Would something like this work?

def word2bytes(n):
"""Given an positive integer n, return a tuple of 4 bytes from
n's least significant 32-bits
"""
r = []
n &= 0xFFFFFFFFL
for i in range(4):
r.append(n & 0xFF)
n >>= 8
r.reverse()
return tuple(r)



--
Jim Dennis,
Starshine: Signed, Sealed, Delivered

Jun 10 '07 #4

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

Similar topics

2
by: David Cook | last post by:
Java's InetAddress class has some methods that use a byte-array to hold what it describes as a 'raw IP address'. So, I assume that they mean an array like: byte ba = new byte; would hold an...
2
by: Andrew | last post by:
Hi I don't know if this is a common question or not I was wondering How I could convert an Integer to a byte in C is there a function that does this and if not could someone explain the math to...
18
by: Ptp | last post by:
is there a integer data type of one byte in gcc? I know delphi have fundamental integer types include byte, shortint... the byte types is unsigned 8-bit, such as below: procedure...
9
by: sudi | last post by:
HI, I face the task of converting a 6 byte integer (MSB first) to a decimal string. The platform I use supports only 4 byte basic data type. I have written some algorithms that loops to many...
26
by: John Grandy | last post by:
Is it possible to generate a 20 byte integer from a GUID that is "unique enough" ( just like a GUID is not truly unique , but is "unique enough" ). We identify transactions with GUIDs , but a...
4
by: Sanjay | last post by:
Hi Is this a legal to convert from byte array to int array.. CopyMemory intAudio(0), byteAudio(0), 4096 Please help me out thanks sanjay
6
by: John Dann | last post by:
I'm trying to use a third party .Net charting control. One of its methods takes a standard 4-byte integer as a parameter (to specify a colour actually, but it's done as a standard integer value and...
7
by: carlospedr | last post by:
I'm sending ESC sequences to a printer, one of these sequences include a parameter that is "2 byte integer", in order to send it to the printer I have to convert an int to 2 byte integer, how do I...
8
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.