Tianyi Shen Tianyi Shen - 5 days ago 4x
Python Question

Pack hex string using struct module?

I want to pack a hex string with python pack.
Here is my code:

import struct
q= ('%x' % int(query, 16)).decode('hex').decode('utf-8')

Query is a hex string.
The code does not work if I change the string to '53040600d0010100' and change 6s to 8s.
Is there any better way to pack such a hex string?


The string gets truncated because you're telling it you want to pack it up to length 6 (6s). You'll have to either raise that number, or work around your string getting truncated.

Also, stop juggling with the encoding of your string, just query.decode('hex') should suffice.