Home>
What i am trying to do
I want to convert a 2-byte hexadecimal number assigned to a variable into a decimal number in Python2.7.
data_hex = rec [-2:]
print ("RX: {0}". format (codecs.encode (data_hex, 'hex_codec')))
Now, the execution result of this program is
RX: 029b.
I tried to convert to decimal with the following code
data_dec = int (data_hex, 16)
print (data_dec)
I got an error.
data_dec = int (data_hex, 16)
ValueError: invalid literal for int () with base 16: '\ x02 \ x9b'
If i write in the value, it can be converted without any problems, but
Is there another way to convert a variable (with the value I want to convert) in place of an int?
data_dec = int ('029b', 16)
print (data_dec)
-
Answer # 1
-
Answer # 2
print (data_hex [0] | (data_hex [1]<<8))
What about?
Related articles
- if the value of the variable to which the variable is assigned is changed in python, the value will change to the original varia
- python 3x - i want to save the value of a variable in memory
- about the phenomenon that the value of the variable (list object including map) is reset in python 38
- i want to dynamically change the value of a variable in python and execute it
- python - filename cannot be assigned to a variable when using a for statement with a shell command in google colaboratory
- python - get the function name and the assigned value to the argument
- python - absolute value behavior of complex numbers in numpy
- c # - how to deposit a value in a static variable and retrieve it again
- (python) i would like to know how to combine an int value and a multidimensional array
- python - (tensorflow/speech recognition) what kind of numerical conversion method is there to enable input of arbitrary wav file
- python - pass a variable to the websocket on_message function
- php - how to call an array value and assign it to a variable
- i want to put it in another variable with python for
- i want to get the value from a constant in python and display it
- i want to set the maximum value of the slider in python to the number entered in the text box
- python - how to output the key and value in the dictionary in any form
- python 3x - how to get the value of scrolledtext
- python - i want to get the same result as assigning a variable and writing it directly in a function
- put the maximum value of the list of variables in the objective function with python pulp
- php - can't the $_post array be assigned to a variable?
Trends
If you want to decode a byte sequence, use the struct module.