Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
How to convert
x = "0x000000001" # hex number string
to
y = "1"
You can do:
y = int("0x000000001", 16)
in your case:
y = int(x, 16)
Looks like you want the int converted to string:
y = str(int(x, 16))
Add a comment
Use int() and provide the base which your number is in (in your case 16). Then apply str() to convert it back to a string:
int()
16
str()
Note: If you omit the base then the default base 10 is used which would result in a ValueError in your case.
10
ValueError
>>> int("0x000000001", 16) 1
Python 2.7 have a problem to convert hex from read binary file
Python 3 not have this problem
f=open(file_name,'rb') raw = f.read() print int(raw[6]) #error invalid literal for int with base 10: print ord(raw[6]) #Work
you can do:
y = int(x, 0) # '0' ;python intepret x according to string 'x'.
In the offical documentation, it is explained as "If base is zero, the proper radix is determined based on the contents of the string;"
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.