converting a string to binary number in python [duplicate] - python

This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 9 years ago.
I want to convert a string consists of binary digits and convert that string into a binary number.
suppose, my input is "01001", this is in string format, i want to convert this to binary number format to perform various bit wise operations on that.

You can convert the number to an int and do bitwise operations, like this
my_int = int("01001", 2)
print my_int & 1 # 1
print my_int & 8 # 8
print my_int & 16 # 0

Related

Float precision in Python [duplicate]

This question already has answers here:
How to round to 2 decimals with Python? [duplicate]
(21 answers)
Closed 2 months ago.
I have a float,
5.8307200000000005e-06
But I only want the precision of 5 on it, so it looks like this
5.83072e-06
How can I do this in Python?
Update, new code
def precision(number):
# The number you want to change the precision of
number
# Convert the number to scientific notation
sci_notation = '{:.5e}'.format(number)
# Split the scientific notation string into its coefficient and
exponent parts
coefficient, exponent = sci_notation.split('e')
# Round the coefficient to 5 decimal places
rounded_coefficient = round(float(coefficient), 5)
# Rebuild the scientific notation string using the rounded
coefficient and the original exponent
rounded_sci_notation = f'{rounded_coefficient}e{exponent}'
# Convert the scientific notation string back to a float
rounded_number = float(rounded_sci_notation)
return rounded_number
Here is how you format a number with 5 significant digits and scientific notation:
number = 5.8307200000000005e-06
print(f"{number:.5e}")
>>> 5.83072e-06
But this does nothing with the precision of the number, just the way it is printed.....

Integer equivalence of binary number that is represented as an integer in Python [duplicate]

This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Closed 5 months ago.
I have an input a=int(1010). I want to find integer equivalence of binary 1010. If I use bin(a) then output will be 1111110010, but I want to get 10.
You need to tell python, that your integer input is in binary. You can either parse a string with a defined base, or ad a 0b-prefix to your code constants.
a = int("1010", base=2)
a = 0b1010
print(a) # result: 10
print(bin(a)) # result: 1010

a basic eight number string to eight bit binary number [duplicate]

This question already has answers here:
Convert base-2 binary number string to int
(10 answers)
Python int to binary string?
(36 answers)
Closed 1 year ago.
eight character (zero and one) string -> turn it into 8 bit binary number -> perform binary operation -> turn back into 8 0/1 string
>>> the_entry = "00000000"
>>> the_binary number = 8CharacterStringIntoThe8CharacterBinaryNumber(the_entry)
>>> the_binary_number << 1 # any binary operation
>>> the_result_string = EightCharacterBinaryNumberIntoEightCharacterString(the_binary_number)
this is soo simple, I refuse to write my own method, but obviously, in th 180000 possible results, I cannot find the one related to my question.
Do you hold the magic??
These are simple basic operations:
entry = "00000000"
number = int(entry, 2)
result = f"{number:08b}"

Convert binary back to ascii in python [duplicate]

This question already has answers here:
Convert binary to ASCII and vice versa
(8 answers)
Closed 4 years ago.
So i have a function that converts an alphabet character to it's binary.
def toBinary(char):
return "".join([format(ord(char), '#010b')[2:]])
For example, toBinary('a') gives me
01100001
How do i convert 01100001 back to the ascii 'a'?
One way could be
c = chr(int(s, 2))
where s is the binary string.
Try This:
chr(int('01100001',2))

Binary in Python [duplicate]

This question already has answers here:
What do numbers starting with 0 mean in python?
(9 answers)
Closed 8 years ago.
When I write run the following code in Python:
m = 010001110110100101110110011001010010000001101101011001010010000001100001011011100010000001000001
print "m = ", m
I receive the output:
m = 7772840013437408857694157721741838884340237826753178459792706472536593002623651807233
What's happening? Is Python automatically converting from base 2 to base 10?
You're starting m with 0. Python assumes it's an Octal (see this)
What you're seeing is the decimal representation of the octal number.
If you want to work with that as binary numbers, I'd recommend setting it as an str and then parse it to int specifying that you're parsing something in base 2:
>>> m='0100011'
>>> int(m, 2)
35
Kind of, it interprets a number starting with 0 as octal, so it actually isn't binary in the first place.
In any case, you can make it output the integer as binary like so:
print "m = ", "{0:b}".format(m)

Categories