6
votes

I want to create a fixed size integer in python, for example 4 bytes. Coming from a C background, I expected that all the primitive types will occupy a constant space in memory, however when I try the following in python:

import sys  
print sys.getsizeof(1000)
print sys.getsizeof(100000000000000000000000000000000000000000000000000000000)

I get

>>>24  
>>>52

respectively.
How can I create a fixed size (unsigned) integer of 4 bytes in python? I need it to be 4 bytes regardless if the binary representation uses 3 or 23 bits, since later on I will have to do byte level memory manipulation with Assembly.

4
Then i think you are using the wrong language for that. If you have to do something in Assembly to optimize speed/memory you should stick with C and not use a semi-interpreted language like python - WindowsMaker
If you are going to do low-level calls to Assembly, start reading up on the ctypes module. - PaulMcG
I agree with saftco. If you really need to do it in python, maybe pass it through a C function before you try to manipulate it with assembly. - Adam Miller
@zaftcoAgeiha Actually there are plenty of tools in Python to accomodate this kind of thing. It's very much designed to be a "glue" language for lower-level languages. Although Adam Miller has the right idea — going through C might be a better idea. - detly
This is a program for the BeagleBoneBlack and all the packages are in python. Therefore generally is easier to stick in python. However, cannot avoid Assembly, because we will have to use the PRU units. Ctypes could be a viable option. I will test it out along with struct.pack, as Simeon Visser suggested. - George

4 Answers

4
votes

the way I do this (and its usually to ensure a fixed width integer before sending to some hardware) is via ctypes

from ctypes import c_ushort 

def hex16(self, data):
    '''16bit int->hex converter'''
    return  '0x%004x' % (c_ushort(data).value)
#------------------------------------------------------------------------------      
def int16(self, data):
    '''16bit hex->int converter'''
    return c_ushort(int(data,16)).value

otherwise struct can do it

from struct import pack, unpack
pack_type = {'signed':'>h','unsigned':'>H',}
pack(self.pack_type[sign_type], data)
5
votes

You can use struct.pack with the I modifier (unsigned int). This function will warn when the integer does not fit in four bytes:

>>> from struct import *
>>> pack('I', 1000)
'\xe8\x03\x00\x00'
>>> pack('I', 10000000)
'\x80\x96\x98\x00'
>>> pack('I', 1000000000000000)
sys:1: DeprecationWarning: 'I' format requires 0 <= number <= 4294967295
'\x00\x80\xc6\xa4'

You can also specify endianness.

1
votes

you are missing something here I think

when you send a character you will be sending 1 byte so even though

sys.getsizeof('\x05') 

reports larger than 8 you are still only sending a single byte when you send it. the extra overhead is python methods that are attached to EVERYTHING in python, those do not get transmitted

you complained about getsizeof for the struct pack answer but accepted the c_ushort answer so I figured I would show you this

>>> sys.getsizeof(struct.pack("I",15))
28
>>> sys.getsizeof(c_ushort(15))
80

however that said both of the answers should do exactly what you want

0
votes

I have no idea if there's a better way to do this, but here's my naive approach:

def intn(n, num_bits=4):
    return min(2 ** num_bits - 1, n)