I've been reading up a lot on stuct.pack and hex and the like.
I am trying to convert a decimal to hexidecimal with 2-bytes. Reverse the hex bit order, then convert it back into decimal.
I'm trying to follow these steps...in python
Convert the decimal value **36895** to the equivalent 2-byte hexadecimal value:
**0x901F**
Reverse the order of the 2 hexadecimal bytes:
**0x1F90**
Convert the resulting 2-byte hexadecimal value to its decimal equivalent:
**8080**
Keep in mind that 'hex'(base 16 0-9 and a-f) and 'decimal'(0-9) are just constructs for humans to represent numbers. It's all bits to the machine.
The python hex(int) function produces a hex 'string' . If you want to convert it back to decimal:
Print formatting also works with strings.
To convert from decimal to hex, use:
That will output the hex value for 255. To convert back to decimal, use
That would output the decimal value for 1F90.
You should be able to reverse the bytes using:
and this would output 1F90. Hope this helps!
My approach
or one line
or with bytearray