Pack hex string using struct module?
<p>I want to pack a hex string with python pack.
Here is my code:</p>

<pre><code>import struct
query='430401005001'
q= ('%x' % int(query, 16)).decode('hex').decode('utf-8')
qpacked=struct.pack('6s',str(q))
</code></pre>

<p>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?</p>

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

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

python string hex