pythonformatdataformat

How to change hexadecimal data format in python?


i am beginner in python sorry... i have a series of data like this

b'B\x86\xfe\xca\x00\x00\x00\x00\x04\x08\x00\x00\xff\xff\xff\xff'

which i convert to hex

['0x42', '0x86', '0xfe', '0xca', '0x0', '0x0', '0x0', '0x0', '0x4', '0x8', '0x0', '0x0', '0xff', '0xff', '0xff', '0xff']

but i want to turn it into something like this

0x4286feca, 0x00004800, 0xffffffff

how can i do this? thanks so much

My code below >

brA = [b for b in struct.unpack(str(len(data))+'B',temp)]
sent_val = [str(hex(v)) for v in brA]

Solution

  • data = b'B\x86\xfe\xca\x00\x00\x00\x00\x04\x08\x00\x00\xff\xff\xff\xff'
    
    dwords_as_str = ["0x" + data[i:i+4].hex() for i in range(0, len(data), 4)]
    print(dwords_as_str)
    

    Output:

    ['0x4286feca', '0x00000000', '0x04080000', '0xffffffff']
    

    See: