0

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]
2
  • Does this answer your question? Python: binary/hex string conversion? Commented Jul 15, 2022 at 9:16
  • Note that CPython can't output bytes or int as hex like you want. You can, of course, create a str that looks like hex. Commented Jul 15, 2022 at 10:26

1 Answer 1

0
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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.