Convert list of ASCII codes to string (byte array) in Python
Question:
I have a list of integer ASCII values that I need to transform into a string (binary) to use as the key for a crypto operation. (I am re-implementing java crypto code in python)
This works (assuming an 8-byte key):
key = struct.pack('BBBBBBBB', 17, 24, 121, 1, 12, 222, 34, 76)
However, I would prefer to not have the key length and unpack() parameter list hardcoded.
How might I implement this correctly, given an initial list of integers?
Thanks!
Answers:
key = "".join( chr( val ) for val in myList )
struct.pack('B' * len(integers), *integers)
*sequence
means “unpack sequence” – or rather, “when calling f(..., *args ,...)
, let args = sequence
“.
I much prefer the array
module to the struct
module for this kind of tasks (ones involving sequences of homogeneous values):
>>> import array
>>> array.array('B', [17, 24, 121, 1, 12, 222, 34, 76]).tostring()
'x11x18yx01x0cxde"L'
no len
call, no string manipulation needed, etc — fast, simple, direct, why prefer any other approach?!
For Python 2.6 and later if you are dealing with bytes then a bytearray
is the most obvious choice:
>>> str(bytearray([17, 24, 121, 1, 12, 222, 34, 76]))
'x11x18yx01x0cxde"L'
To me this is even more direct than Alex Martelli’s answer – still no string manipulation or len
call but now you don’t even need to import anything!
This is reviving an old question, but in Python 3, you can just use bytes
directly:
>>> bytes([17, 24, 121, 1, 12, 222, 34, 76])
b'x11x18yx01x0cxde"L'
Shorter version of previous using map()
function (works for python 2.7):
"".join(map(chr, myList))
I have a list of integer ASCII values that I need to transform into a string (binary) to use as the key for a crypto operation. (I am re-implementing java crypto code in python)
This works (assuming an 8-byte key):
key = struct.pack('BBBBBBBB', 17, 24, 121, 1, 12, 222, 34, 76)
However, I would prefer to not have the key length and unpack() parameter list hardcoded.
How might I implement this correctly, given an initial list of integers?
Thanks!
key = "".join( chr( val ) for val in myList )
struct.pack('B' * len(integers), *integers)
*sequence
means “unpack sequence” – or rather, “when calling f(..., *args ,...)
, let args = sequence
“.
I much prefer the array
module to the struct
module for this kind of tasks (ones involving sequences of homogeneous values):
>>> import array
>>> array.array('B', [17, 24, 121, 1, 12, 222, 34, 76]).tostring()
'x11x18yx01x0cxde"L'
no len
call, no string manipulation needed, etc — fast, simple, direct, why prefer any other approach?!
For Python 2.6 and later if you are dealing with bytes then a bytearray
is the most obvious choice:
>>> str(bytearray([17, 24, 121, 1, 12, 222, 34, 76]))
'x11x18yx01x0cxde"L'
To me this is even more direct than Alex Martelli’s answer – still no string manipulation or len
call but now you don’t even need to import anything!
This is reviving an old question, but in Python 3, you can just use bytes
directly:
>>> bytes([17, 24, 121, 1, 12, 222, 34, 76])
b'x11x18yx01x0cxde"L'
Shorter version of previous using map()
function (works for python 2.7):
"".join(map(chr, myList))