def trans(x):
if x == 0: return [0]
bit = []
while x:
bit.append(x % 2)
x >>= 1
return bit[::-1]
Answer from Jun HU on Stack Overflowdef trans(x):
if x == 0: return [0]
bit = []
while x:
bit.append(x % 2)
x >>= 1
return bit[::-1]
Just for fun - the solution as a recursive one-liner:
def tobin(x):
return tobin(x/2) + [x%2] if x > 1 else [x]
Converting Binary to Decimal in python (without built in binary function) - Stack Overflow
Convert decimal to binary in python - Stack Overflow
python - how can I convert binary to decimal without using bin method - Stack Overflow
How to Convert list of binary number to decimal in python without using any inbuilt function?
To convert from binary to decimal, just do:
int(binary_string, 2) # The 2 is the base argument from which we convert.
Demo:
>>> int('0b10110', 2)
22
Note- There are many issues with the code you are using to convert decimal to binary. If you are insistent on not using a built-in function for that purpose, you may be interested by this post:
Convert an integer to binary without using the built-in bin function
Although personally if I were to want to avoid the bin() function, I would do something like:
"{0:#b}".format(an_integer)
Demo:
>>> "{0:#b}".format(22)
'0b10110'
This is much more Pythonic than your current code.
this would be a lot easier no?
conv = str(input('what do you want to convert?'))
if conv == 'binary':
x = str(input('enter binary : '))
ansBin = int(x, 2)
print('decimal for binary ', x, ' is ', ansBin)
elif conv == 'decimal':
y = int(input('enter decimal : '))
ansDec = bin(y)[2:]
print('binary for decimal ', y, ' is ', ansDec)
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
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
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}"
For example: [1,0,0,1] should give output 9