22

In Python (3) at least, if a binary value has an ASCII representation, it is shown instead of the hexadecimal value. For instance, the binary value of 67 which is ASCII C is show as follows:

bytes([67]) # b'C'

Whereas for binary values without ASCII representations, they are shown in hex. I.E.

b'\x0f'

Is there a way to force Python to show the binary values in their binary-hex form (if this is what it is called), even when there are ASCII representations?

Edit: By this I mean, something that starts with b'\x',. This would make debugging easier when you are looking for specific bytes to be printed for instance.

Thanks

6
  • 1
    @BrenBarn I don't think it's a duplicate of that question. This question is asking about how to get a hex-for-everything as a repr (or some other string transformation) while the other question/answer mainly supposes the difference and addresses the equivalency thereof. Commented Oct 25, 2014 at 23:21
  • @user2864740: The answer, though, includes a way to display hex values of all bytes. Commented Oct 25, 2014 at 23:25
  • 1
    I did see that question, but I was thinking along the same lines. In that question the accepted answer does a good job of explaining that they are the same and why they are printed different, but does not explain how to essentially "turn off" repr's ASCII formatting. I know they are the same, but during debugging, it is more difficult to continually convert. Even using .encode('hex') as that answer mentions does not format the characters the same (i.e. prefixing them with \x Commented Oct 25, 2014 at 23:25
  • @Startec: If it's important to you to specifically have the \x, you should probably edit your question to say that (and to explain why). Commented Oct 25, 2014 at 23:26
  • Yes, but that answer shows hex values, not binary representations...Which are indeed different. '7919' vs b'\x79\x19' Commented Oct 25, 2014 at 23:26

2 Answers 2

6

After installing my package all-escapes there will be a new codec available for this usage.

>>> b = bytes([10,67,128])
>>> print(b.decode("all-escapes"))
\x0a\x43\x80
Sign up to request clarification or add additional context in comments.

Comments

4

There is no specific means of requiring any particular formatting (like \x) for a byte string. If you really need specific formatting, you could use something like the .hex() solution from this question, but wrap it with other code to insert the formatting you need. Another useful tool is the hex builtin function. For instance, if you want \x:

>>> x = bytes([67, 128])
>>> print(''.join(r'\x'+hex(letter)[2:] for letter in x))
\x43\x80

If you just need to be able to visually distinguish the bytes, using hex by itself may work for you (it uses 0x instead of \x):

>>> print(''.join(hex(letter) for letter in x))
0x430x80

There is not a way to make this the default behavior for byte strings. Whatever you do, you're going to have to write code that specifies the display format you want; you can't make Python automatically display printable bytes as \x escapes.

1 Comment

This will fail however for characters with a hex value < x10 though. if you do x = bytes([10,67,128]) you will get \xa\x43\x80 which isn't valid it needs to be \x0a\x43\x80 I would recommend changing to this: print(''.join('\\x{:02x}'.format(letter) for letter in x))

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.