Communities for your favorite technologies. Explore all Collectives
Ask questions, find answers and collaborate at work with Stack Overflow for Teams.
Ask questions, find answers and collaborate at work with Stack Overflow for Teams. Explore Teams
Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
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;"
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.