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)
It looks like you want the int to convert to a string:
y = str(int(x, 16))
Use int() and specify the base where your number is located (in your case 16 ).Then apply str() to convert it to a string:
int()
16
str()
Note. If you omit the base, the default base is 10 , which will result in a ValueError in your case.
10
ValueError
>>> int("0x000000001", 16) 1
Python 2.7 has a problem converting hex from a binary read file
Python 3 does 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
y = int(x, 0) # '0' ;python intepret x according to string 'x'.
In official documentation, this is explained as follows: "If the base is zero, then the correct root is determined based on the contents of the string;"
Source: https://habr.com/ru/post/1306901/More articles:Forced to use too many hidden fields; finding an alternative approach - javascriptExcel VBA for SqlServer - excel-vbajQuery + Simple dialog - javascriptJava proxy dynamic issues - javaGo - Data types to validate - validationDjango as a S3 proxy - djangoDictionary binding in Silverlight using INotifyPropertyChanged function - c #MySQL Select statement Where table1.id! = Table2.id - joinhow to find out when / where to call an overridden method of a superclass - javaXML schema - how to relate the existence of one attribute to the existence of another attribute - xmlAll Articles