How can I convert a string to its byte value? I have a string "hello" and I want to change is to something like "/x68...".
3 Answers
Python 2.6 and later have a bytearray type which may be what you're looking for. Unlike strings, it is mutable, i.e., you can change individual bytes "in place" rather than having to create a whole new string. It has a nice mix of the features of lists and strings. And it also makes your intent clear, that you are working with arbitrary bytes rather than text.
4 Comments
easysid
quote "I want to change all what is in file (String) into byte array. " .. @kindall's answer does exactly that. +1 for bytearray()
Trevor Rudolph
list(bytearray("hello"))Trevor Rudolph
I did some speed testing and
list(bytearray("hello")) is faster than map(ord, "hello")kindall
Not converting it to a list (leaving it as a
bytearray) is generally even faster!Perhaps you want this (Python 2):
>>> map(ord,'hello')
[104, 101, 108, 108, 111]
For a Unicode string this would return Unicode code points:
>>> map(ord,u'Hello, 马克')
[72, 101, 108, 108, 111, 44, 32, 39532, 20811]
But encode it to get byte values for the encoding:
>>> map(ord,u'Hello, 马克'.encode('chinese'))
[72, 101, 108, 108, 111, 44, 32, 194, 237, 191, 203]
>>> map(ord,u'Hello, 马克'.encode('utf8'))
[72, 101, 108, 108, 111, 44, 32, 233, 169, 172, 229, 133, 139]
2 Comments
user100464
Note that this will not return "byte" values for non-ASCII characters, because ord will return something bigger than 0xFF.
Mark Tolonen
@user100464, not for a byte string, which the above is for Python 2 (
map doesn't return a list in Python 3). A byte can't be above 0xFF. Also, non-ASCII is above 0x7F, not 0xFF :)
"hello"and"\x68\x65\x6C\x6C\x6F"are identical (unless you escape the backslashes instead of using them for hex escapes)?strtype in Python basically is just a set of bytes (meaning that it doesn't have a representation, like Unicode, attached and can just be an arbitrary sequence of bytes, despite its name).stris unicode. To convert it to bytes, dos.encode()(you can also specify what character encoding you want, otherwise it will use UTF-8).