0
binary_str = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

How can I convert this binary data to hex data in python?

I tried like this but it's not working.

>>> hex(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'bytes' object cannot be interpreted as an integer
Random Davis
  • 6,662
  • 4
  • 14
  • 24
  • 2
    what would be your expected output? – norok2 Mar 11 '21 at 16:16
  • Are you trying to convert it to a hex string? Or to raw bytes that you could then potentially display as hex data? You need to be more clear and detailed. – Random Davis Mar 11 '21 at 16:17
  • I am not sure. This is converted from a integer 10. It should give hex value – Kiran S youtube channel Mar 11 '21 at 16:18
  • @RandomDavis. can you please explain both ? – Kiran S youtube channel Mar 11 '21 at 16:18
  • 1
    @KiranSyoutubechannel Is [this](https://stackoverflow.com/questions/6624453/whats-the-correct-way-to-convert-bytes-to-a-hex-string-in-python-3) what you want? Also, I'm not going to explain those basic concepts to you, that's something you need to research on your own. We on StackOverflow expect you to do the basic research beforehand - see: [How much research effort is expected of Stack Overflow users](https://meta.stackoverflow.com/q/261592/6273251) – Random Davis Mar 11 '21 at 16:20
  • The `b` doesn't stand for binary, but for bytes... Can you show how you get `binary_str` and what is your expected output? – Tomerikoo Mar 11 '21 at 16:21
  • By the way `binary_str` is already hex represented as bytes. `\x` denotes that the next two characters should be parsed as hex values. – Giorgos Myrianthous Mar 11 '21 at 16:21
  • @GiorgosMyrianthous. can you please answer it how it will give \x value ? – Kiran S youtube channel Mar 11 '21 at 16:23
  • I think `binary_str = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'` binary_str isn't actually binary... I agree with @GiorgosMyrianthous Your value is hex not binary.... –  Mar 11 '21 at 16:33

1 Answers1

0

In python, hex does not work like that. Try it like this:

binary_str = b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'

print(binary_str.hex())
The Pilot Dude
  • 2,091
  • 2
  • 6
  • 24