0

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!

Selcuk
  • 57,004
  • 12
  • 102
  • 110
Tony B
  • 21
  • 2
  • 2
    You shouldn't really be using Python 2.x as it is not supported anymore. – Selcuk Sep 22 '21 at 06:22
  • Thanks for the info but the script will be used on our testers that has Python 2.x installed. I don't have permission to update or install the latest one. – Tony B Sep 23 '21 at 08:11

0 Answers0