I have an integer - say for example
a = 12345
b = hex(a)
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;
I need an array something like this - [0x39, 0x30]
Currently I am converting the whole number into a string then to a list and then doing some element of list manipulation.
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.
If you need a list of hex strings with number as little endian bytes:
a=12345
l = [hex(a&0xff),hex(a>>8)] # little endian format, as hex string
print(l)
gives:
['0x39', '0x30']
note the quotes, it is not possible to print [0x39, 0x30]
. If you want the integer values, just do
l = [a&0xff,a>>8] # little endian, format as bytes
which gives:
[57, 48]
and BTW:
[0x39, 0x30]==[57, 48] => True
it's just that representation/print of integers in a list is in decimal :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.