Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- #converts denary to binary/octal/hex
- def BaseConversion(n):
- binary = bin(n)[2:]
- octal = oct(n)[2:]
- hexadecimal = hex(n)[2:]
- print("Binary:",binary)
- print("Oct:",octal)
- print("Hex:",hexadecimal)
- return binary, octal, hexadecimal
- #converts binary numbers to denary
- def BinToDec(n):
- list_dec=[]
- for i in range(0,len(n)):
- list_dec.append(int(n[::-1][i])*2**i)
- return(sum(list_dec))
- #converts strings to ASCII
- def TexttoAscii(n):
- binstr=""
- for char in n:
- pad = ""
- byte=str(bin(ord(char))[2:])
- while len(byte) < 8:
- byte = "0" + byte
- binstr += byte+" "
- return binstr
- #converts ASCII binary to text strings
- def AsciitoText(n):
- count = 0
- word = ""
- conv = ""
- for num in n:
- if (num == "1") or (num == "0"):
- count +=1
- word += num
- if count == 8:
- conv += chr(BinToDec(word))
- count = 0
- word = ""
- elif num == " ":
- continue
- else:
- print("Error: This is not a valid binary number")
- return conv
- #create a text file and write to it
- def TextFileCreation():
- return null
- def Menu():
- print("Built in functions demo")
- print("""
- 1. Denary to base 2, 8, 16
- 2. Binary to denary
- 3. Text to ASCII binary
- 4. ASCII binary to text
- 5. WIP TextFileCreation
- """)
- try:
- choice = int(input("Type the number of the function you require:\n"))
- if choice == 1:
- BaseConversion(int(input("Enter a denary number to convert:\n")))
- sleep(1)
- elif choice == 2:
- print(BinToDec(input("Enter a binary number:\n")))
- sleep(1)
- elif choice == 3:
- print(TexttoAscii(input("Enter a text string\n")))
- sleep(1)
- elif choice == 4:
- print(AsciitoText(input("Enter a binary string\n")))
- sleep(1)
- elif choice == 5:
- print("Not completed yet")
- sleep(1)
- else:
- print("Error, try again")
- except ValueError:
- print("Woops, you need to enter a number from the menu")
- Menu()
- Menu()
Add Comment
Please, Sign In to add comment