21

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...".

11
  • 2
    You realize that it's all just bits and bytes at the lowest level and that the strings "hello" and "\x68\x65\x6C\x6C\x6F" are identical (unless you escape the backslashes instead of using them for hex escapes)? Commented Dec 20, 2010 at 15:15
  • 1
    This makes no sense, what do you actually want to do? (This is just some intermediate step you think you need to do) Commented Dec 20, 2010 at 15:20
  • 7
    What are you trying to do, exactly? It's worth noting that the str type 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). Commented Dec 20, 2010 at 15:24
  • 4
    We should add: if you're using Python 3, str is unicode. To convert it to bytes, do s.encode() (you can also specify what character encoding you want, otherwise it will use UTF-8). Commented Dec 20, 2010 at 15:40
  • 3
    For those who are wondering why you would want to do this: I found this to be useful for parsing binary data read using pySerial. Commented Feb 27, 2015 at 21:05

3 Answers 3

33

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.

Sign up to request clarification or add additional context in comments.

4 Comments

quote "I want to change all what is in file (String) into byte array. " .. @kindall's answer does exactly that. +1 for bytearray()
list(bytearray("hello"))
I did some speed testing and list(bytearray("hello")) is faster than map(ord, "hello")
Not converting it to a list (leaving it as a bytearray) is generally even faster!
28

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

Note that this will not return "byte" values for non-ASCII characters, because ord will return something bigger than 0xFF.
@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 :)
14

If you want to get hexadecimal string representation you could do:

"hello".encode("hex") # '68656c6c6f'

And to meet your reference representation (don't take it seriously, guess this is not what you really want):

"".join(["/x%02x" % ord(c) for c in "hello"]) # '/x68/x65/x6c/x6c/x6f'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.