Pro Girl
Pro Girl

Reputation: 952

How to convert with Python this hex to its decoded output?

I'm trying to convert the following (that seems to be an HEX) with Python with its decoded output:

I want to convert this:

enter image description here

To this: enter image description here

How to do this?

This is the string:

0x00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000006331b7e000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000474657374000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007566963746f727900000000000000000000000000000000000000000000000000

Upvotes: 1

Views: 206

Answers (2)

Code-Apprentice
Code-Apprentice

Reputation: 83517

First you need to convert the hex into a bytearray:

hex = 0x00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000006331b7e000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000474657374000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007566963746f727900000000000000000000000000000000000000000000000000
b = bytearray.fromhex(hex).decode()

Then you will need to determine the layout of the bytes. For example, an unit256 is probably 32 bytes which is 64 hex digits:

a = b[:64]
print(int.from_bytes(a, "big"))

Here I assume the bytes are in big-endian. If they are instead little-endian, you can use "little" instead of "big". You will need to learn about so-called "endianness" to understand this better.

You can get the other uint256 in a similar way.

As for the strings, I don't know what their length is. You will have to research the format for Ethereum blockchain data. Once you determine the length, you can use a similar technique to get the bytes for each string and then decode it into characters.

Upvotes: 1

ChessAndCode
ChessAndCode

Reputation: 111

Just use the inbuilt decode function in Python:

str="0x00000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000006331b7e000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000474657374000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000007566963746f727900000000000000000000000000000000000000000000000000" #YOUR HEX
str.decode("hex")

Alternatively, if that does not work, you can use: bytearray.fromhex(str).decode()

Upvotes: 1

Related Questions