Python 3.2+ converting int to bytes
<p>I'm having a problem converting int to bytes in Python.  </p>

<p>This works - </p>

<pre><code>&gt;&gt;&gt; (1024).to_bytes(2, 'big')
b'\x04\x00'  
</code></pre>

<p>However this does not work as I would expect - </p>

<pre><code>&gt;&gt;&gt; (33).to_bytes(2, 'big')
b'\x00!'
</code></pre>

<p>What am I not understanding?</p>

<p>You're not understanding that <code>!</code> is ASCII character 33, equivalent to <code>\x21</code>. This bytestring is exactly the bytestring you asked for; it just isn't displayed the way you were expecting.</p>

python python-3.2