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?

MightyPork
  • 18,270
  • 10
  • 79
  • 133

2 Answers2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 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 Nov 19 '14 at 17:57
  • @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. – Martijn Pieters Nov 19 '14 at 17:58
  • @MightyPork: see the [literal notation specification](https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals); 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. – Martijn Pieters Nov 19 '14 at 18:00
  • 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.. – MightyPork Nov 19 '14 at 18:02
  • Right. Also see [What does a b prefix before a python string mean?](http://stackoverflow.com/q/2592764) then. :-) – Martijn Pieters Nov 19 '14 at 18:03
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'>
RubenLaguna
  • 21,435
  • 13
  • 113
  • 151