Simple but require bit manipulation Python Hex and INT
<p>I have an integer - say for example </p>

<pre><code>a = 12345
b = hex(a)
</code></pre>

<p>I have to send this to an MCU in a specific format. First I have to convert it into hex. which I did and it gives me --- 0x3039; </p>

<p>I need an array something like this - [0x39, 0x30]</p>

<p>Currently I am converting the whole number into a string then to a list and then doing some element of list manipulation. </p>

<p>I am hoping there is something easier. Which can be done in a line or two? It should work even with the following numbers - 1234, 123, 12, 1- Implying it should work from single digit to five digit numbers. </p>

<p>If you need a list of hex strings with number as little endian bytes:</p>

<pre><code>a=12345
l = [hex(a&amp;0xff),hex(a&gt;&gt;8)]  # little endian format, as hex string
print(l)
</code></pre>

<p>gives:</p>

<pre><code>['0x39', '0x30']
</code></pre>

<p>note the quotes, it is not possible to print <code>[0x39, 0x30]</code>. If you want the integer values, just do</p>

<pre><code>l = [a&amp;0xff,a&gt;&gt;8]  # little endian, format as bytes
</code></pre>

<p>which gives:</p>

<pre><code>[57, 48]
</code></pre>

<p>and BTW:</p>

<pre><code>[0x39, 0x30]==[57, 48] =&gt; True
</code></pre>

<p>it's just that representation/print of integers in a list is in decimal :)</p>

python arrays string int