Is it possible to pack 4 chars into an int in python and have it be represented as one value in a bytearray? I'm looking into packing it into a struct, but run into an error if I try something like
characters = b'abcd'
struct.pack('i',characters)
Is it possible to pack 4 chars into an int in python and have it be represented as one value in a bytearray? I'm looking into packing it into a struct, but run into an error if I try something like
characters = b'abcd'
struct.pack('i',characters)
I'm not certain that I understand your problem -- If you want to take a bytestring and turn it into an integer, on python3.x, this is pretty easy:
import sys
value = int.from_bytes(b'abcd', sys.byteorder) # for me, byteorder is `'little'`
assert value == 1684234849
On python2.x, you'd unpack the struct (which will work for python3.x too):
value, = struct.unpack('<i', b'abcd')
assert value == 1684234849
Now if you want to put this value into a bytearray -- The answer is no, you can't do that. bytearray
only accepts values 0
-> 255