how many bytes in an int

R

Reid Nichol

Hello,
I was wondering if I could control how many bytes are in an int and
the byte order. In C/C++ I can use int32 but how do I do this in
python? How can I control byte order?
 
G

Grant Edwards

I was wondering if I could control how many bytes are in an int and
the byte order. In C/C++ I can use int32 but how do I do this in
python? How can I control byte order?

I suspect you want to use the "struct" module -- but it's a
guess, since you haven't really said what it is you're trying
to accomplish.
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Reid said:
I was wondering if I could control how many bytes are in an int and
the byte order. In C/C++ I can use int32 but how do I do this in
python? How can I control byte order?

You can't. Python uses as many bytes as are necessary to represent
the number (larger numbers - more bytes). Why do you want to control
the number of bytes?

Regards,
Martin
 
R

Reid Nichol

Grant said:
I suspect you want to use the "struct" module -- but it's a
guess, since you haven't really said what it is you're trying
to accomplish.

I'm thinking of writing a movie file encoder (probably avi). So, I need
to output DWORD (lookup revealed its a 4-byte int) to a binary file.
Therefore I need to know whether this can be done in python or not,
which will tell me whether I'll try to do it or not.

But, since the 64-bit archecture is out, short, long, etc may change
there meanings quite soon. From what I've read in the struct module
docs I can only tell it that it's a short, long, etc. but not whether
it's exactly a 4-byte int. Is there a way to do this?
 
G

Grant Edwards

I'm thinking of writing a movie file encoder (probably avi).
So, I need to output DWORD (lookup revealed its a 4-byte int)
to a binary file. Therefore I need to know whether this can
be done in python or not, which will tell me whether I'll try
to do it or not.

But, since the 64-bit archecture is out, short, long, etc may
change there meanings quite soon. From what I've read in the
struct module docs I can only tell it that it's a short, long,
etc. but not whether it's exactly a 4-byte int. Is there a
way to do this?

The struct module is the only thing I know about. If you're
worried about the "C" types in the struct module changing
underneat you, you could do a pure Python implimentation of
"python-int" to/from DWORD. It's utterly trivial and shouldn't
take more than one or two lines of code.
 
G

Grant Edwards

The struct module is the only thing I know about. If you're
worried about the "C" types in the struct module changing
underneat you, you could do a pure Python implimentation of
"python-int" to/from DWORD. It's utterly trivial and shouldn't
take more than one or two lines of code.

def toLittleEndianDWORD(i):
return ''.join(map(chr,[x&0xff for x in [i,i>>8,i>>16,i>>24]]))

def fromLittleEndianDWORD(s):
return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

Reid said:
I'm thinking of writing a movie file encoder (probably avi). So, I need
to output DWORD (lookup revealed its a 4-byte int) to a binary file.
Therefore I need to know whether this can be done in python or not,
which will tell me whether I'll try to do it or not.

You looked up DWORD somewhat incorrectly. It is a four byte int in
memory, but on disk, it is a little-endian byte string of four bytes.

So you *do* need the struct module, because only that will give you
byte strings (of course, Grant's formula also works)
But, since the 64-bit archecture is out, short, long, etc may change
there meanings quite soon. From what I've read in the struct module
docs I can only tell it that it's a short, long, etc. but not whether
it's exactly a 4-byte int. Is there a way to do this?

As Grant says: use the struct module. Use struct.calcsize to find out
how large an int is. If the size is too large, try a short. If the size
is too small, try a long. If no type matches, take the next larger type,
and drop the extra bytes.

However, it does not actually need to be that difficult: "int" is 32-bit
on all current systems, including all 64-bit systems (only long is
64-bits on some 64-bit systems).

Regards,
Martin
 
R

Robin Becker

Grant said:
The struct module is the only thing I know about. If you're
worried about the "C" types in the struct module changing
underneat you, you could do a pure Python implimentation of
"python-int" to/from DWORD. It's utterly trivial and shouldn't
take more than one or two lines of code.


def toLittleEndianDWORD(i):
return ''.join(map(chr,[x&0xff for x in [i,i>>8,i>>16,i>>24]]))

def fromLittleEndianDWORD(s):
return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)

this simple code will give warnings in 2.3, I think struct is really needed.
__main__:2: FutureWarning: x<<y losing bits or changing sign will return
a long in Python 2.4 and up
-1
 
G

Grant Edwards

Grant said:
The struct module is the only thing I know about. If you're
worried about the "C" types in the struct module changing
underneat you, you could do a pure Python implimentation of
"python-int" to/from DWORD. It's utterly trivial and shouldn't
take more than one or two lines of code.


def toLittleEndianDWORD(i):
return ''.join(map(chr,[x&0xff for x in [i,i>>8,i>>16,i>>24]]))

def fromLittleEndianDWORD(s):
return ord(s[0]) + (ord(s[1])<<8) + (ord(s[2])<<16) + (ord(s[3])<<24)

this simple code will give warnings in 2.3,

I think struct is really needed.

Like the man said, "struct" doesn't convert to-from integers of
specified byte lengths. All it has are the C types "int"
"long" "long long", etc. There is no portable way using struct
to request a 4-byte integer.
__main__:2: FutureWarning: x<<y losing bits or changing sign will return
a long in Python 2.4 and up
-1

Hopefully that can be fixed? Python integer objects seem to
get more difficult to work with every year. ;) A few weeks
back, somebody posted code for fixed length Python integer
objects.
 
G

Grant Edwards

Like the man said, "struct" doesn't convert to-from integers
of specified byte lengths. All it has are the C types "int"
"long" "long long", etc. There is no portable way using
struct to request a 4-byte integer.

I like the "calcsize" suggestion for "portablizing" the struct
method. Once at program startup you figure out what struct
formats you need for various lengths and Bob's your uncle.
 
R

Richard Brodie

By guessing formats and calculating sizes? Or is there a way to
ask for an N-byte integer that I missed?

If you use '<' or '>' to force endianness you automatically get 'standard'
sizes thrown in; or you can use '=' for native order. The standard sizes are
specified in the struct module documentation.
 
R

Reid Nichol

Martin said:
As Grant says: use the struct module. Use struct.calcsize to find out
how large an int is. If the size is too large, try a short. If the size
is too small, try a long. If no type matches, take the next larger type,
and drop the extra bytes.

However, it does not actually need to be that difficult: "int" is 32-bit
on all current systems, including all 64-bit systems (only long is
64-bits on some 64-bit systems).

Regards,
Martin

Thanks to all who helped. This is my solution (maybe overkill) but I
plan on adding read/write functions, etc (of course its just a first
thought). It seems to work, so any feedback is appreciated.

#!/usr/bin/env python
from struct import *

class FixedSizeInteger:
def __init__(self, length_in_bytes, endianness='@'):
self.fmt = ''
self.endianness = endianness

# find the length_in_bytes byte datatype
for type in ['h', 'i', 'l', 'q']:
if length_in_bytes == calcsize(type):
self.fmt = endianness + type

# should throw an exception here
if self.fmt == '':
print 'ERROR: type not found'

def printFmt(self):
print 'My format is ' + self.fmt


if __name__ == '__main__':
test = FixedSizeInteger(4)

test.printFmt()
 
G

Grant Edwards

If you use '<' or '>' to force endianness you automatically
get 'standard' sizes thrown in; or you can use '=' for native
order. The standard sizes are specified in the struct module
documentation.

Doh! I don't know how many times I've read that without
realizing what it meant. If you specify byte order explicitly
you _do_ get guaranteed lengths.
 
G

Grant Edwards

Thanks to all who helped. This is my solution (maybe overkill) but I
plan on adding read/write functions, etc (of course its just a first
thought). It seems to work, so any feedback is appreciated.

As was just pointed out to me, if you specify byter order with
'<' or '>', you don't have to guess/calculate lengths, they are
fixed regardless of the underlying C types. IOW, struct does
do exactly what you want. Just specify the byte order you desire.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top