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>According to documentation -> <a href="https://docs.python.org/3.3/library/stdtypes.html" rel="nofollow">https://docs.python.org/3.3/library/stdtypes.html</a></p>

<pre><code> &gt;&gt;&gt; (1024).to_bytes(2, byteorder='big')
 b'\x04\x00'
 &gt;&gt;&gt; (1024).to_bytes(10, byteorder='big')
 b'\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00'
 &gt;&gt;&gt; (-1024).to_bytes(10, byteorder='big', signed=True)
 b'\xff\xff\xff\xff\xff\xff\xff\xff\xfc\x00'
 &gt;&gt;&gt; x = 1000   
 &gt;&gt;&gt; x.to_bytes((x.bit_length() // 8) + 1, byteorder='little')
 b'\xe8\x03'
</code></pre>

python python-3.2