I have a 64-bit binary string and I want to convert it to hexadecimal.
Here's is my simple code.
The [2:].upper is to remove the "0x" characters from the output value.
bnum = "0100000000000000000000000000000000000000000000000000000000000000"
hdnum = hex(int((bnum),2))[2:].upper()
print("Hex Number: ",hdnum)
Output: ('Hex Number: ', '4000000000000000')
For this example, the output '4000000000000000' is correct. However, when I simply changed the bnum value by setting the 1st bit to "1" and the rest to "0")
bnum = "1000000000000000000000000000000000000000000000000000000000000000"
hdnum = hex(int((bnum),2))[2:].upper()
print("Hex Number: ",hdnum)
Output: ('Hex Number: ', '8000000000000000L')
The output '8000000000000000L' is now wrong because of the additional "L" character at the end.
May I please know what's wrong with my hex() function usage and how to fix it? Thanks!