473,659 Members | 2,980 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16319
On 2004-08-09, Reid Nichol <rn*********@ya hoo.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*********@ya hoo.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*********@ya hoo.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.co m> 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 toLittleEndianD WORD(i):
return ''.join(map(chr ,[x&0xff for x in [i,i>>8,i>>16,i> >24]]))

def fromLittleEndia nDWORD(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.co m> 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 toLittleEndianD WORD(i):
return ''.join(map(chr ,[x&0xff for x in [i,i>>8,i>>16,i> >24]]))

def fromLittleEndia nDWORD(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.
toLittleEndianD WORD(-1) '\xff\xff\xff\x ff' fromLittleEndia nDWORD(toLittle EndianDWORD(-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***@SPAMREMO VEjessikat.fsne t.co.uk> wrote:
Grant Edwards wrote:
On 2004-08-09, Grant Edwards <gr****@visi.co m> 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 toLittleEndianD WORD(i):
return ''.join(map(chr ,[x&0xff for x in [i,i>>8,i>>16,i> >24]]))

def fromLittleEndia nDWORD(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.
toLittleEndianD WORD(-1) '\xff\xff\xff\x ff' fromLittleEndia nDWORD(toLittle EndianDWORD(-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.co m> 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 "portablizi ng" 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

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

Similar topics

10
3278
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 'sizeof(char)'. It seems that I could use bitset<8> to represent a byte in my code --- if you have a better suggestion, I welcome it --- but that still leaves me with the question of how to write those bitsets to an image file as big-endian bytes...
19
5853
by: Lorenzo J. Lucchini | last post by:
My code contains this declaration: : typedef union { : word Word; : struct { : byte Low; : byte High; : } Bytes; : } reg;
8
1927
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 Condition_String2 allocates the 72 bytes, I am not able to get the Artimatic Numbers to staisfy the above Number of bytes allocated,
1
1431
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 the server, where the server responds back with the same # bytes. i set this value to 441 bytes (so that it is an odd number). the server receives 8,000 bytes. always. no matter what. every single time. i send 441 bytes 1 time and the server...
13
2422
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 how many bytes are actually sent. I can detect abort of the script using a shutdown handler. In the shutdown handler, I tried ob_get_length, but it returns false. I tried to read the server's log file, but it is does not contain the...
5
3653
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 written in file are not the sent bytes. Copy and paste the following lines to observe my problem. What can I do to resolve problem ? Only System.Text.Encoding.ASCII write the same number of bytes, but not the good bytes. Someone can help me. Thanks by...
4
4711
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 can't do something easy like putting the bytes into a long or uint and shifting that. Let me give an example: If I want to shift this: 10010110 00001101
7
2504
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_ will be 8 bytes on a 64 bit system versus 4 bytes on a 32 bit one. So what gives? Consider: The two listings below came from compiling the following console
11
3582
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 Read_bin(ByVal ruta As String) Dim cadena As String = "" Dim dato As Array If File.Exists(ruta) = True Then
9
3840
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 = file.read() print "bytes read ", len(data) ---
0
8428
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8335
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8747
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6179
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2752
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 we have to send another system
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.