This post is read-only. Explore Repls and connect with other creators on Community.View Community
The info in this post might be out of date, check out our docs instead. View docs
4

# Binary

JustineHoresh

Hello folks. I need a python program that is converting a number in binary like 2=10.
Thanks in advance for your help and sorry for my broken english

4 years ago

### Voters

TopNew
1
alpedhuezdewey735

7 months ago
1

Decimal To Binary:

print("Converting Decimal Numbers To Binary\n\n")
a=int(input("Enter The Number: "))
b=a%2
print(b)
c=(a//2)%2
print(c)
d=(a//4)%2
print(d)
e=(a//8)%2
print(e)
f=(a//16)%2
print(f)
g=(a//32)%2
print(g)
h=(a//64)%2
print(h)
i=(a//128)%2
print(i)
print("The binary number of the decimal number", a, "is\n", i,h,g,f,e,d,c,b)

Binary To Decimal:

a=str(input("Enter The Number: "))
b=(a[7:8])
c=(a[6:7])
d=(a[5:6])
e=(a[4:5])
f=(a[3:4])
g=(a[2:3])
h=(a[1:2])
i=(a[0:1])
j=int(b)
k=int(c)
l=int(d)
m=int(e)
n=int(f)
o=int(g)
p=int(h)
q=int(i)
print("The Decimal Number Equal To The Entered Binary Number Is", (j2**0)+(k21)+(l*22)+(m2**3)+(n24)+(o*25)+(p2**6)+(q2**7), "\b.")

Completely Working Code...

11 months ago
1
finleyparr

num = int(input('Input number between 0 and 255 and I will convert it into binary.'))
if num < 256:
print(bin(num))

this code it quite short but it has an annoying 0b at the start and i dont know how to remove it, but as long as you can ignore it or can remove it yourself, feel free to use it, it took me a couple minutes to make

4 years ago
0
PabloJuan

@finleyparr

bin(int(x)).split('0b')[1]

2 years ago
1

@finleyparr
Use StrSlicing Code

First Take The Length By Len(#The Number or Variable Here.)
Then Take #The Variable or Number[3:#The Length]

11 months ago
1
olafh10

I created some code for you: https://repl.it/@olafh10/int-to-binary

This converts the input to binary, note that the program doesn't check if the input is a number. You can also just copy the intToBinary function for in your own program.

4 years ago