ethereum / hexbytes

Python `bytes` subclass that decodes hex, with a readable console output
MIT License
27 stars 19 forks source link

Printing HexBytes shows the native bytes representation instead of the hex one #4

Closed mydapp closed 6 years ago

mydapp commented 6 years ago

I want to change a HexByte value " (b"\x03\x08wf\xbfh\xe7\x86q\xd1\xeaCj\xe0\x87\xdat\xa1'a\xda\xc0 \x01\x1a\x9e\xdd\xc4\x90\x0b\xf1;") " to "'0x03087766bf68e78671d1ea436ae087da74a12761dac020011a9eddc4900bf13b'", it can diplay on consoleline, it will not work .I can not print a HexByte liikethis : a = HexBytes(b"\x03\x08wf\xbfh\xe7\x86q\xd1\xeaCj\xe0\x87\xdat\xa1'a\xda\xc0 \x01\x1a\x9e\xdd\xc4\x90\x0b\xf1;") print(a) it will not work

carver commented 6 years ago

At the moment, only the repr() shows the HexBytes version. str() still shows the native bytes representation.

At a command line, you can do:

>>> a = HexBytes(b"\x03\x08wf\xbfh\xe7\x86q\xd1\xeaCj\xe0\x87\xdat\xa1'a\xda\xc0 \x01\x1a\x9e\xdd\xc4\x90\x0b\xf1;")
>>> a
HexBytes('0x03087766bf68e78671d1ea436ae087da74a12761dac020011a9eddc4900bf13b')

Or in a script you can do:

a = HexBytes(b"\x03\x08wf\xbfh\xe7\x86q\xd1\xeaCj\xe0\x87\xdat\xa1'a\xda\xc0 \x01\x1a\x9e\xdd\xc4\x90\x0b\xf1;")
assert repr(a) == "HexBytes('0x03087766bf68e78671d1ea436ae087da74a12761dac020011a9eddc4900bf13b')"