最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to make python print bytes strings as hex code all the way? - Stack Overflow

programmeradmin0浏览0评论

So I want to turn a positive integer into a little endian byte string of length 2.

Easy! For example, 200:

>>> b = int.to_bytes(200, 2, 'little')
b'\xc8\x00'

However, as soon as we take one that may be interpreted as some ASCII symbol, like, e.g., 100:

>>> b = int.to_bytes(100, 2, 'little')
b'd\x00'
>>> print(b)  #<< Does not help
b'd\x00'

^Ugly! Barely readable in my opinion! Is there a straight-forward way to tell python to make it hex-style all the way? Like so:

>>> b = int.to_bytes(100, 2, 'little')
b'\x64\x00'

P.S.: For clarification of the question: It is an easy task to write a function that will do just what I want, printing the bytes as desired, b'\x64\x00'-style-all-the-way-hex.

The question here is: Is there an option to bring the python interpreter to doing this for me? Like logging.basicConfig(..) only interpreter.basicNumericFormat(bytes, ..). And if so: How would one achieve that?

So I want to turn a positive integer into a little endian byte string of length 2.

Easy! For example, 200:

>>> b = int.to_bytes(200, 2, 'little')
b'\xc8\x00'

However, as soon as we take one that may be interpreted as some ASCII symbol, like, e.g., 100:

>>> b = int.to_bytes(100, 2, 'little')
b'd\x00'
>>> print(b)  #<< Does not help
b'd\x00'

^Ugly! Barely readable in my opinion! Is there a straight-forward way to tell python to make it hex-style all the way? Like so:

>>> b = int.to_bytes(100, 2, 'little')
b'\x64\x00'

P.S.: For clarification of the question: It is an easy task to write a function that will do just what I want, printing the bytes as desired, b'\x64\x00'-style-all-the-way-hex.

The question here is: Is there an option to bring the python interpreter to doing this for me? Like logging.basicConfig(..) only interpreter.basicNumericFormat(bytes, ..). And if so: How would one achieve that?

Share Improve this question edited Feb 6 at 6:36 Markus-Hermann asked Feb 5 at 13:32 Markus-HermannMarkus-Hermann 99713 silver badges27 bronze badges 5
  • I mean, .hex() exists. – Mippy Commented Feb 5 at 13:37
  • So it does. But I want it as a little endian "bytes" string on the console. E.g., hex(1024)==0x400 is scarce a replacement for \x00\x04! Especially if we are dealing with really large numbers and order is important for the user's reading pleasure! – Markus-Hermann Commented Feb 5 at 13:40
  • Did you see my answer, it should have an output with the string you desire. – Mippy Commented Feb 5 at 13:48
  • 1 Why do you want b'\x64\x00, what are you going to do with it? – no comment Commented Feb 5 at 14:22
  • I want to review the output while hacking inside a console, preferring pure hex format. And I want to learn how to customize console output. – Markus-Hermann Commented Feb 6 at 9:01
Add a comment  | 

1 Answer 1

Reset to default 1

If you want the output you gave an example of, you can loop through the bytes and style them in a string. Here's an example:

b = int.to_bytes(100, 2, 'little')
formatted = ''.join(f'\\x{byte:02x}' for byte in b)
print(formatted)

However, if you just want the hex, you can use .hex() to convert it to a hex.

b = int.to_bytes(100, 2, 'little')
print(b.hex())
发布评论

评论列表(0)

  1. 暂无评论