all numbers are stored in binary. if you want a textual representation of a given number in binary, use bin(i)
>>> bin(10)
'0b1010'
>>> 0b1010
10
Answer from aaronasterling on Stack Overflowall numbers are stored in binary. if you want a textual representation of a given number in binary, use bin(i)
>>> bin(10)
'0b1010'
>>> 0b1010
10
Without the 0b in front:
"{0:b}".format(int_value)
Starting with Python 3.6 you can also use formatted string literal or f-string, --- PEP:
f"{int_value:b}"
python - Converting binary to decimal integer output - Stack Overflow
Converting from decimal to binary in Python
Converting Binary to Decimal in python (without built in binary function) - Stack Overflow
Convert decimal input to 8 bit binary
Videos
You can use int and set the base to 2 (for binary):
>>> binary = raw_input('enter a number: ')
enter a number: 11001
>>> int(binary, 2)
25
>>>
However, if you cannot use int like that, then you could always do this:
binary = raw_input('enter a number: ')
decimal = 0
for digit in binary:
decimal = decimal*2 + int(digit)
print decimal
Below is a demonstration:
>>> binary = raw_input('enter a number: ')
enter a number: 11001
>>> decimal = 0
>>> for digit in binary:
... decimal = decimal*2 + int(digit)
...
>>> print decimal
25
>>>
Binary to Decimal
int(binaryString, 2)
Decimal to Binary
format(decimal ,"b")
ps: I understand that the author doesn't want a built-in function. But this question comes up on the google feed even for those who are okay with in-built function.
Would someone be willing to help me (total beginner) understand how this converts decimal values to binary? Maybe just by explaining what happens line by line?
def dec2bin(n):
if n > 1:
dec2bin(n//2)
print(n % 2, end="")It's just not intuitive at all for me :/
Any help would be greatly appreciated!
you can use this code:
binary= raw_input("Binary: ")
d= int(binary, 2)
print d
To convert binary value to decimal you need to do the following:
Take the least significant bit and multiply it by 2^0, then take the next least significant beat and multiply it by 2^1, next one by 2^2 and so on...
Let's say, for example you need to convert a number 1010 to decimal:
You would have 0*2^0 + 1*2^1 + 0*2^2 + 1*2^3 = 0 + 2 + 0 + 8 = 10
So in your python code, you need to:
- read the int that the user inputted (representing the binary value).
- convert that int and convert it to string, so you can break it into list of digits
- make a list of digits from the string you created (a list int python can be created from a string not an int, that's why you need the conversion to string first)
- go trough that list of bits in reverse and multiply every bit by 2^k, k being the counter starting from 0
Here's the code that demonstrates what I just tried to explain:
user_input = int(input("enter a binary value"))
bits = list(str(user_input))
decimal = 0
counter = 0
for i in reversed(bits):
decimal += 2**counter * int(i)
counter+=1
print 'The decimal value is: ', decimal
Hi all i'm very new to coding (a few weeks) and Python and was wondering if there is a way to get an input of a decimal number and convert it to 8 bit binary. Currently using the Thonny IDE