I have a list of integers:
var = [1, 5, 4, 17, 231, 89]
If I wanted to convert it to a list of bytes instead, I can do:
[(i).to_bytes(1, byteorder='big') for i in var]
Since each value in var
is less than 256, it can fit in one byte per integer. But if I had another list, say:
var = [1, 2, 15, 12]
This can fit an integer into half a byte, or more accurate to what I'm looking for, fit two integers per byte.
How can I specify to combine two integers into a byte if possible, both to and from?
Something like below:
var1 = [1, 5, 4, 17, 231, 89]
var2 = [1, 2, 15, 12]
def foo(var):
if max(var) < 2**4:
num_bytes = 0.5
elif max(var) < 2**8:
num_bytes = 1
elif max(var) < 2**16:
num_bytes = 2
if num_bytes >= 1:
return [(i).to_bytes(num_bytes, byteorder='big') for i in var], num_bytes
elif num_bytes = 0.5:
# convert var to list of nybbles
# combine two nybbles to a byte
# create list of bytes (length is half length of var)
# return it and num_bytes
def unfoo(var, num_bytes):
if num_bytes >= 1:
print([int.from_bytes(i, 'big') for i in var])
elif num_bytes = 0.5:
# print original list of integers again
I want to convert a list of integers into a list of bytes, but fitting two nybbles to a byte if it can fit, then do the conversion back.
Desired outcome is:
a, b = foo(var1)
unfoo(a, b) # prints [1, 5, 4, 17, 231, 89]
a, b = foo(var2)
unfoo(a, b) # prints [1, 2, 15, 12]
I don't want a list of smallest number of bits to represent a single number. Note the max(list)
: if all values in the list can be 8-bits, fit it to 8-bits; if all values can be 16-bits, fit it to 16-bits; if all values can be a nybble, then make two nybble pairs into a list of bytes.
Basically if I have two integers that can fit in a nybble each, how do I concatenate both into a single byte? If I know that bytes need to be split into two, how can I do the split? I can always assume the original list will be divisible by 2.