This question already has answers here:
Converting integer to binary in python
(17 answers)
Convert base-2 binary number string to int
(10 answers)
Closed 6 years ago.
I don't know much about Python, so sorry if this sounds silly. I am getting a string of 0s and 1s from an Arduino, and I need to convert it to a 'regular' number. I've found out how to convert a binary string to a decimal integer pretty easily, but how would I convert it if my binary string is stored in a variable ?
Let's say I've got a=00000110
int(a, 2) doesn't work (tells me "int() can't convert non-string with explicit base")
Is there a specific syntax that I should be aware of ? Can't I put a variable in here ?
Thanks in advance !
Related
This question already has an answer here:
Python 3, Converting a string storing binary data to Int
(1 answer)
Closed 2 years ago.
Convert binary number to integer in python?
like if there is any inbuilt function to do so?
Any ideas?
bno = '1001'
int(bno, 2)
This can be used
This question already has answers here:
How to suppress scientific notation when printing float values?
(16 answers)
Closed 3 years ago.
When dividing 2 large numbers in Python the output was 1.5640891676957637e+308. How do I print the entire number?
This can't be done because that's all the precision the float type has. It tells you the first 16 or something digits of your number and the exponent, but the digits after the first 16 have never been calculated because there's no space for them.
If you want to work with huge numbers and have basically infinite precision, almost like with Python's integers, try the SymPy library.
number_str = str(int(1.5640891676957637e+308))
print(number_str)
Prints:
156408916769576373071379516999674270294758197183972476505692635672561429946607721482839700799900977784426920800145985096418278978450607600874550703086464871105809270941181641564392002031609107640705147719606017681794554578537463358952125037388161745430586972528713238507284919924435316681000630776819257180160
You can consider the decimal data type in the standard library https://docs.python.org/3/library/decimal.html "The decimal module provides support for fast correctly-rounded decimal floating point arithmetic. It offers several advantages over the float datatype...Decimal numbers can be represented exactly"
This question already has answers here:
python3: How to get logical complement (negation) of a binary number, eg. '010' => '101'?
(5 answers)
Two Complement's Python (with as least bits as possible)
(1 answer)
Closed 4 years ago.
I need to convert negative number to binary in python.
How can I do it ?
EDIT: I need to use twos complement
bin(-2)
#'-0b10'
Use the built in binfunction to get a binary representation.
This question already has answers here:
Convert fraction to float?
(9 answers)
Closed 8 years ago.
How to change a '14/15' string to a float?
I am trying to extract data from a text file would like to convert '1/3' to a float. float('1/3') doesn't work. I was thinking about splitting into two parts at '/' by 1 and 3 then dividing, but it seems cludgy. Is there a more pythonic way to do this? I'm using Python 2.7
If you only ever need to evaluate simple X/Y fractions:
s = "14/15"
num, denom = map(float, s.split("/", 1))
print(num / denom)
If you need a more complete expression evaluator, take a look at the asteval module.
Using eval() might also see like a nice easy way to do it, but I'd advise against it for security reasons.
If you trust your input:
from __future__ import division
eval('14/15')
This question already has answers here:
Change default float print format
(8 answers)
Closed 8 years ago.
In c++, there exists the cout.precision() to set the number of decimal digits I want to see when I print out a floating point number. Is there any equivalent solution in Python so that every time I want to print out a float I don't get those 11 extra digits that I don't actually need?
In Python you can use the format() function your floats explicitly:
print format(floatvalue, '.5f')
to output a float with 5 digits after the decimal. See the Format Specification Mini-Language for details on the formatting options.
You can also use the str.format() method to control how a float value is interpolated into a larger string, using the same formatting parameters.