10

I have a bytes object:

a =  b'067b'

How do I get a string from it? Like this:

"067b"

I've tried:

In [3]: str(a)
Out[3]: "b'067b'"

In [4]: import codecs

In [5]: codecs.decode(a,'hex')
Out[5]: b'\x06{'

In [6]: import binascii

In [7]: binascii.b2a_hex(a)
Out[7]: b'30363762'

In [8]: binascii.hexlify(a)
Out[8]: b'30363762'

Is there no way to do this?

2 Answers 2

9

You simply want to decode from ASCII here, your bytestring is already representing hexadecimal numbers, in ASCII characters:

>>> a = b'067b'
>>> a.decode('ascii')
'067b'

Everything you tried is interpreting the bytes as numeric data instead, either as hexadecimal numbers representing bytes or as bytes representing numeric data.

So your first attempt takes the value 06 as a hexadecimal number and turns that into the byte value 6, and 7b is turned into the byte value 123, which is the ASCII codepoint for the { character.

In your second attempt you are converting each byte to a hexadecimal representation of its numeric value. The 0 byte being interpreted as the integer number 48 (the ASCII codepoint for the '0' character), which is 30 in hexadecimal. '6' is 54, or 36 in hex, etc.

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

5 Comments

This works, but I don't see how b'067b' is ASCII. Isn't it really two bytes, '06' and '7b'? That'd give me indeed '\x06{' if taken as ASCII values. What's really going on here?
@MightyPork: no, it is four bytes. The characters 0, 6, 7 and b. The b'...' literal notation does not take hexadecimal characters, it takes ASCII characters.
@MightyPork: see the literal notation specification; if you meant to specify two bytes, you can use b'\x06\x7b' (e.g. use \xhh hex escapes), or use the bytes() function and pass in a sequence of integers: bytes([0x06, 0x7b]), using integers specified as hex literals.
I got this from pyudev and wanted to display it. I'm not building it. This was just the first time I've seen the b'blah' notation..
9

The bytes representation can be confusing. b'0607' is not the same as bytes([0x06,0x07]). b'0607' is actually 4 bytes [48,54,48,55] where 48 is the ascii code for 0,etc.

[x for x in b'0607'] 
[48, 54, 48, 55]

as such b'0607' is already an ascii string but represented as bytes if you want it to convert it to str you need to b'0607'.decode("ascii") (so that you construct a str from those bytes interpreting them like ascii code (as they are)).

In general, you convert bytes to hex string like this:

>>> a = binascii.hexlify(bytes([1,10,15,16,255]))
>>> a
b'010a0f10ff'
>>> type(a)
<class 'bytes'>
>>> b = a.decode("ascii")
>>> type(b)
<class 'str'>

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.