Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert string to byte array in Python

Tags:

python

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?

like image 604
AndroidDev Avatar asked Jul 24 '12 04:07

AndroidDev


People also ask

How do you create a byte array from a string in Python?

Python binary string to byte array In this example, I have taken a binary string as string = “11000010110001001100011”. To convert the binary string to a byte array. I have used new_string = bytearray(string, “ascii”). The bytearray() method returns the byte array object.

How do I convert string to bit in Python?

To convert a string to binary, we first append the string's individual ASCII values to a list ( l ) using the ord(_string) function. This function gives the ASCII value of the string (i.e., ord(H) = 72 , ord(e) = 101). Then, from the list of ASCII values we can convert them to binary using bin(_integer) .

Can we convert string to byte?

A String is stored as an array of Unicode characters in Java. To convert it to a byte array, we translate the sequence of characters into a sequence of bytes. For this translation, we use an instance of Charset. This class specifies a mapping between a sequence of chars and a sequence of bytes.


7 Answers

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])
like image 108
avasal Avatar answered Oct 05 '22 12:10

avasal


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.

like image 24
Pithikos Avatar answered Oct 05 '22 11:10

Pithikos


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

like image 38
Jon Perryman Avatar answered Oct 05 '22 12:10

Jon Perryman


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

like image 33
mgear Avatar answered Oct 05 '22 12:10

mgear


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

like image 40
juliocesar Avatar answered Oct 05 '22 11:10

juliocesar


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')
like image 44
oldpride Avatar answered Oct 05 '22 10:10

oldpride


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

If you want hex:

print map(hex, a)
like image 30
HYRY Avatar answered Oct 05 '22 11:10

HYRY