I want to convert any value (that can be negative or positive) into hex. My current method does this.
The read value in this example is 4003.
workingline = stringdb.readline().split(";")
print hex(int(workingline[0]))
This returns
0xfa3
It should be:
0xa30f0000
(Padded with zeros and inverted hexadecimal) IF the value is negative it should be:
0xFEFFFFFF
With the value -2.
I assume padding does not help in this case.
Thank you!
解决方案
You want the struct module:
>>> struct.pack("
'a30f0000'
For -2, you'll need to do some other work:
>>> struct.pack("
'feffffff'
A way to do it for any value is:
struct.pack("