Skip to content

Instantly share code, notes, and snippets.

@goxr3plus
Last active November 8, 2018 14:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goxr3plus/12242d8950ae887d8a8271e685b5e455 to your computer and use it in GitHub Desktop.
Save goxr3plus/12242d8950ae887d8a8271e685b5e455 to your computer and use it in GitHub Desktop.
Python Binary to Decimal , Python Decimal to Binary
``` PYTHON
#----------------Convert Binary to Decimal and check for validity----------------------
def isBinary(s):
try:
return set(s) <= set('01')
except ValueError:
return False
t = 1
while t > 0:
binaryNumber = input("Enter a binary No.")
if(isBinary(binaryNumber)):
print("It is binary")
else:
continue
convertedNumber = int(binaryNumber, 2)
print(convertedNumber)
print("")
#----------------------Convert Decimal to Binary and check for validity----------------------
def RepresentsInt(s):
try:
int(s)
return True
except ValueError:
return False
t = 1
while t > 0:
binaryNumber = input("Enter a decimal No.")
if ( RepresentsInt(binaryNumber) ):
print("It is Number")
else:
continue
convertedNumber = bin(int(binaryNumber))
print(convertedNumber[2:])
print("")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment