Search code examples
pythoncurlhexbinarystream

Convert binary string to hex for curl request


I have a struct pack like

def encode_number(value):
    return struct.pack('>I', value)


def decode_number(raw):
    return int.from_bytes(raw, byteorder='big') 

>>> encode_number(1) b'\x00\x00\x00\x01'

>>> decode_number(b'\x00\x00\x00\x01') 1

while the transcations given via curl are given in Hex like curl http://localhost:26657/broadcast_tx_commit?tx=0x01

So 0x01 is 1 in hex,how do you convert your binary string to hex easily for curl command ? (preferable in a linux terminal)


Solution

  • Just did a google search and came across this: https://unix.stackexchange.com/questions/65280/binary-to-hexadecimal-and-decimal-in-a-shell-script

    The example they use for Binary to Hex (Bash/Linux Terminal):

    $ printf '%x\n' "$((2#101010101))"
    155