473,320 Members | 2,180 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,320 software developers and data experts.

how many bytes in an int

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?
Jul 18 '05 #1
15 16256
On 2004-08-09, Reid Nichol <rn*********@yahoo.com> wrote:
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.

--
Grant Edwards grante Yow! My polyvinyl cowboy
at wallet was made in Hong
visi.com Kong by Montgomery Clift!
Jul 18 '05 #2
Reid Nichol wrote:
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
Jul 18 '05 #3
Grant Edwards wrote:
On 2004-08-09, Reid Nichol <rn*********@yahoo.com> wrote:

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.


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?
Jul 18 '05 #4
On 2004-08-09, Reid Nichol <rn*********@yahoo.com> wrote:
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?


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.

--
Grant Edwards grante Yow! LOOK!!! I'm WALKING
at in my SLEEP again!!
visi.com
Jul 18 '05 #5
On 2004-08-09, Grant Edwards <gr****@visi.com> wrote:
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)

--
Grant Edwards grante Yow! Well, O.K. I'll
at compromise with my
visi.com principles because of
EXISTENTIAL DESPAIR!
Jul 18 '05 #6
Reid Nichol wrote:
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
Jul 18 '05 #7
Grant Edwards wrote:
On 2004-08-09, Grant Edwards <gr****@visi.com> wrote:

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.
toLittleEndianDWORD(-1) '\xff\xff\xff\xff' fromLittleEndianDWORD(toLittleEndianDWORD(-1)) __main__:2: FutureWarning: x<<y losing bits or changing sign will return
a long in Python 2.4 and up
-1

--
Robin Becker
Jul 18 '05 #8
On 2004-08-09, Robin Becker <ro***@SPAMREMOVEjessikat.fsnet.co.uk> wrote:
Grant Edwards wrote:
On 2004-08-09, Grant Edwards <gr****@visi.com> wrote:

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.
toLittleEndianDWORD(-1) '\xff\xff\xff\xff' fromLittleEndianDWORD(toLittleEndianDWORD(-1))

__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.

--
Grant Edwards grante Yow! YOU PICKED KARL
at MALDEN'S NOSE!!
visi.com
Jul 18 '05 #9
On 2004-08-09, Grant Edwards <gr****@visi.com> wrote:
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.


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.

--
Grant Edwards grante Yow! FIRST, I'm covering
at you with OLIVE OIL and
visi.com PRUNE WHIP!!
Jul 18 '05 #10

"Grant Edwards" <gr****@visi.com> wrote in message
news:41**********************@newsreader.visi.com. ..
Like the man said, "struct" doesn't convert to-from integers of
specified byte lengths.


It does if you ask it to.
Jul 18 '05 #11
On 2004-08-09, Richard Brodie <R.******@rl.ac.uk> wrote:

"Grant Edwards" <gr****@visi.com> wrote in message
news:41**********************@newsreader.visi.com. ..
Like the man said, "struct" doesn't convert to-from integers of
specified byte lengths.


It does if you ask it to.


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

--
Grant Edwards grante Yow! .. this must be what
at it's like to be a COLLEGE
visi.com GRADUATE!!
Jul 18 '05 #12

"Grant Edwards" <gr****@visi.com> wrote in message
news:41**********************@newsreader.visi.com. ..
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.

Jul 18 '05 #13
Martin v. Löwis wrote:
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()
Jul 18 '05 #14
On 2004-08-09, Richard Brodie <R.******@rl.ac.uk> wrote:
"Grant Edwards" <gr****@visi.com> wrote in message
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.


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.

--
Grant Edwards grante Yow! ... I want FORTY-TWO
at TRYNEL FLOATATION SYSTEMS
visi.com installed withinSIX AND A
HALF HOURS!!!
Jul 18 '05 #15
On 2004-08-09, Reid Nichol <rn*********@yahoo.com> wrote:
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.

--
Grant Edwards grante Yow! I call it a "SARDINE
at ON WHEAT"!
visi.com
Jul 18 '05 #16

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

Similar topics

10
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not...
19
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
8
by: ranjeet.gupta | last post by:
Dear All I am not able o understand the exact number of bytes allocation done by the two fucntion given below, It is said that the fuction Condition_String1 allocates the 240 bytes while...
1
by: | last post by:
one thing to keep in mind is that i've written multiple tcp apps in .net 2002 and 2003, and haven't seen this problem before. I have a simple socket app where a client sends some # of bytes to...
13
by: Shailesh Humbad | last post by:
Here is an advanced PHP question. Can anyone think of a way to detect the number of bytes written to output when a script is aborted? I am sending a large file to the client, and I want to record...
5
by: philip | last post by:
Here is some lines of code than I wrote. You can copy/paste theis code as code of form1 in a new project. My problem is this one : I try to write in a file a serie of bytes. BUT some bytes...
4
by: Lee Crabtree | last post by:
I need to shift all of the values in a byte array by more than 8 bits, meaning that values should flow from one byte to another. Since I don't know in advance how many bits will be shifting, I...
7
by: BogusException | last post by:
Why is it that VS 2005, VC++.NET needs 12 bytes to store an int, when it can be done natively with 4? Mind you, even on 64-bit systems the int is still supposed to be 4 bytes. An yes, the _pointer_...
11
by: Freddy Coal | last post by:
Hi, I'm trying to read a binary file of 2411 Bytes, I would like load all the file in a String. I make this function for make that: '-------------------------- Public Shared Function...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.