hex() argument cannot be converted to hex

13,789

Solution 1

pin = hex(int("2FFF6996".replace('L', '').upper()[2:].zfill(8), 16))

>>> pin
'0xff6996'

you can remove upper and hex if you want a number from string to pass it to print function like a %X parameter:

>>> pin = int("2FFF6996".replace('L', '')[2:].zfill(8), 16)
>>> pin
16738710
>>> print "0x%X" % pin
0xFF6996

Solution 2

Hex expects a number so valid options are:

hex(0x2FFF6996) # hex
hex(805267862) # 10 base
hex(05777664626) # 8 base

These all generate the following result: '0x2fff6996'

Share:
13,789
Jake Evans
Author by

Jake Evans

Updated on June 05, 2022

Comments

  • Jake Evans
    Jake Evans almost 2 years

    I am trying to do the following;

    pin = hex("2FFF6996").replace('L', '').upper()[2:].zfill(8)
    

    However I am getting an error - hex() argument cannot be converted to hex.

    Any help would be appreciated!

  • Jake Evans
    Jake Evans over 10 years
    Thanks, this worked. However I am passing the pin variable to another function, which takes %X. Not sure I am doing it right, as I get a TypeError - %X format: a number is required, not a str
  • ndpu
    ndpu over 10 years
    @TomSothcott leave only int: pin = int("2FFF6996".replace('L', '').upper()[2:].zfill(8), 16)