This question already has answers here:
Converting hex string representation to float in python
(2 answers)
Closed 2 years ago.
Is there a way to reverse the hex() method of a float in Python? For example,
n = 1280.03125
n_hex = n.hex()
print(n_hex) # result--> 0x1.4002000000000p+10
How can I convert 0x1.4002000000000p+10 back to 1280.03125? I know you can use int(num, base) to convert a number to integer but it doesn't support decimal.
Try float.fromhex(str):
>>> float.fromhex("0x1.4002000000000p+10")
1280.03125
Related
This question already has answers here:
How to convert an integer to a string in any base?
(35 answers)
Closed 2 years ago.
I want to take this string '01101011' as an 0b01101011 integer in Python.I couldn't find any method to do that.I tried:
a=a+"0b"
a=int(a)
But is has no use.Can you help me guys?
String to bitstring:
Convert the string to integer then to binarystring.
>>> a_str = '101011'
>>> a_bit_str = bin(int('101011',2))
>>> a_bit_str
'0b101011'
String to Integer:
If you just want to convert string to integer
>>> int(a_str,2)
43
This question already has answers here:
Why does "bytes(n)" create a length n byte string instead of converting n to a binary representation?
(16 answers)
Closed 2 years ago.
Given a 8-bytes long signed value, like 3576757170468630901, I would like to convert it to ASCII binarized hexadecimal:
For example:
>> hex(3576757170468630901).encode('ascii')
b'0x31a331b2319d3175'
What I am looking for is the following format:
b'\x31\xa3\x31\xb2\x31\x9d\x31\x75'
I am not sure how can I generate it? Should I break each block and convert it myself?
In python 3 there is now to_bytes() which may help here:
v = 3576757170468630901
print(hex(v))
print(v.to_bytes(8, 'big'))
Output:
0x31a331b2319d3175
b'1\xa31\xb21\x9d1u'
This question already has answers here:
Python: Converting string into decimal number
(8 answers)
Closed 4 years ago.
I have a number 3.8148116e-09
How do I convert it to a real number without the - ?
Thanks.
You can try:
>>> a = "3.8148116e-09"
>>> number = float(a)
>>> print "{:1.16f}".format(number)
0.0000000038148116
The first line parses the string as a number. If you need to print the number or format it for another reason, you can use string#format.
This question already has answers here:
How can I convert a string with dot and comma into a float in Python
(9 answers)
Closed 4 years ago.
my question is simple.
I got my string :
a = '0,0127'
I want to convert it to a number but when i compile
float(a)
i got the following message error :
ValueError: could not convert string to float: '0,0127'
Is there another way to convert it to a number ?
Using str.replace
Ex:
a = '0,0127'
print(float(a.replace(",", ".")))
Output:
0.0127
The reason this isn't working is because the decimal type only recognizes periods (.) for the decimal delimiter as this is what is common in, e.g., english. You could manually change the string or do
a = a.replace(",", ".")
float(a)
Which should work.
This question already has answers here:
How to suppress scientific notation when printing float values?
(16 answers)
Closed 8 years ago.
I have a very silly question, suppose if i have a number 1.70000043572e-05 how should I convert it into float i.e. 0.0000170000043572.
You need to convert to a float and use str.format specifying the precision:
In [41]: print "{:f}".format(float("1.70000043572e-05"))
0.000017
# 16 digits
In [38]: print "{:.16f}".format(float("1.70000043572e-05"))
0.0000170000043572
Just calling float would give 1.70000043572e-05.
Using older style formatting:
In [45]: print( "%.16f" % float("1.70000043572e-05"))
0.0000170000043572
If you are just inputting the number into your script python will understand that as a float.
If it is a string then use the builtin float on it to do the conversion for example:
x = float("0.423423e4")
print "%.2f" % (x)
will output
4234.23