How to convert string to byte array in Python

Python

Python Problem Overview


Say that I have a 4 character string, and I want to convert this string into a byte array where each character in the string is translated into its hex equivalent. e.g.

str = "ABCD"

I'm trying to get my output to be

array('B', [41, 42, 43, 44])

Is there a straightforward way to accomplish this?

Python Solutions


Solution 1 - Python

encode function can help you here, encode returns an encoded version of the string

In [44]: str = "ABCD"

In [45]: [elem.encode("hex") for elem in str]
Out[45]: ['41', '42', '43', '44']

or you can use array module

In [49]: import array

In [50]: print array.array('B', "ABCD")
array('B', [65, 66, 67, 68])

Solution 2 - Python

Just use a bytearray() which is a list of bytes.

Python2:

s = "ABCD"
b = bytearray()
b.extend(s)

Python3:

s = "ABCD"
b = bytearray()
b.extend(map(ord, s))

By the way, don't use str as a variable name since that is builtin.

Solution 3 - Python

An alternative to get a byte array is to encode the string in ascii: b=s.encode('ascii').

Solution 4 - Python

This works for me (Python 2)

s = "ABCD"
b = bytearray(s)

# if you print whole b, it still displays it as if its original string
print b

# but print first item from the array to see byte value
print b[0]

Reference: http://www.dotnetperls.com/bytes-python

Solution 5 - Python

This work in both Python 2 and 3:

>>> bytearray(b'ABCD')
bytearray(b'ABCD')

Note string started with b.

To get individual chars:

>>> print("DEC HEX ASC")
... for b in bytearray(b'ABCD'):
...     print(b, hex(b), chr(b))
DEC HEX ASC
65 0x41 A
66 0x42 B
67 0x43 C
68 0x44 D

Hope this helps

Solution 6 - Python

Depending on your needs, this can be one step or two steps

  1. use encode() to convert string to bytes, immutable
  2. use bytearray() to convert bytes to bytearray, mutable
s="ABCD"
encoded=s.encode('utf-8')
array=bytearray(encoded)

The following validation is done in Python 3.7

>>> s="ABCD"
>>> encoded=s.encode('utf-8')
>>> encoded
b'ABCD'
>>> array=bytearray(encoded)
>>> array
bytearray(b'ABCD')

Solution 7 - Python

s = "ABCD"
from array import array
a = array("B", s)

If you want hex:

print map(hex, a)

Solution 8 - Python

Since none of the answers is producing exactly array('B', [41, 42, 43, 44]) and the answer by avasal fails in Python 3, I post here my alternative:

import array
s = 'ABCD'
a = array.array('B', [ord(c) for c in s])
print(a)

which prints

array('B', [65, 66, 67, 68])

Note that 65-68 is the correct ASCII for "ABCD".

Solution 9 - Python

for python 3 it worked for what @HYRY posted. I needed it for a returned data in a dbus.array. This is the only way it worked

s = "ABCD"

from array import array

a = array("B", s)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndroidDevView Question on Stackoverflow
Solution 1 - PythonavasalView Answer on Stackoverflow
Solution 2 - PythonPithikosView Answer on Stackoverflow
Solution 3 - PythonJon PerrymanView Answer on Stackoverflow
Solution 4 - PythonmgearView Answer on Stackoverflow
Solution 5 - PythonjuliocesarView Answer on Stackoverflow
Solution 6 - PythonoldprideView Answer on Stackoverflow
Solution 7 - PythonHYRYView Answer on Stackoverflow
Solution 8 - Pythonuser171780View Answer on Stackoverflow
Solution 9 - PythonJuly MarvalView Answer on Stackoverflow