Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- path = 'F:\python ascii encouder'
- name = input("Enter filename of test file: ")
- with open(f'{name}.txt', 'w') as fp:
- pass
- rerun = 'Y'
- binaryList = []
- while rerun.upper() == 'Y': # runs the program on a while loop until the user tells it to stop
- phrase = input("Enter your phrase: ")
- count = 0 # initialization of variables for the binary to hexadecimal part
- a = 0
- b = 1
- error = 0
- bin = []
- hexASCII = 0
- decimal = 0
- binary = 0
- while error != 1: # while loop that reads each binary number per 4 digits and converts it into a hexadecimal equivalent
- count += 1
- if phrase[a:b] == 'A':
- hexASCII = 41
- decimal = 65
- binary = '0100 0001'
- elif phrase[a:b] == 'B':
- hexASCII = 42
- decimal = 66
- binary = '0100 0010'
- elif phrase[a:b] == 'C':
- hexASCII = 43
- decimal = 67
- binary = '0100 0011'
- elif phrase[a:b] == 'D':
- hexASCII = 44
- decimal = 68
- binary = '0100 0100'
- elif phrase[a:b] == 'E':
- hexASCII = 45
- decimal = 69
- binary = '0100 0101'
- elif phrase[a:b] == 'F':
- hexASCII = 46
- decimal = 70
- binary = '0100 0110'
- elif phrase[a:b] == 'G':
- hexASCII = 47
- decimal = 71
- binary = '0100 0111'
- elif phrase[a:b] == 'H':
- hexASCII = 48
- decimal = 72
- binary = '0100 1000'
- elif phrase[a:b] == 'I':
- hexASCII = 49
- decimal = 73
- binary = '0100 1001'
- elif phrase[a:b] == 'J':
- hexASCII = '4A'
- decimal = 74
- binary = '0100 1010'
- elif phrase[a:b] == 'K':
- hexASCII = '4B'
- decimal = 75
- binary = '0100 1011'
- elif phrase[a:b] == 'L':
- hexASCII = '4C'
- decimal = 76
- binary = '0100 1100'
- elif phrase[a:b] == 'M':
- hexASCII = '4D'
- decimal = 77
- binary = '0100 1101'
- elif phrase[a:b] == 'N':
- hexASCII = '4E'
- decimal = 78
- binary = '0100 1110'
- elif phrase[a:b] == 'O':
- hexASCII = '4F'
- decimal = 79
- binary = '0100 1111'
- elif phrase[a:b] == 'P':
- hexASCII = 43
- decimal = 80
- binary = '0101 0000'
- elif phrase[a:b] == 'Q':
- hexASCII = 43
- decimal = 81
- binary = '0101 0001'
- elif phrase[a:b] == 'R':
- hexASCII = 43
- decimal = 82
- binary = '0101 0010'
- elif phrase[a:b] == 'S':
- hexASCII = 43
- decimal = 83
- binary = '0101 0011'
- elif phrase[a:b] == 'T':
- hexASCII = 43
- decimal = 84
- binary = '0101 0100'
- elif phrase[a:b] == 'U':
- hexASCII = 43
- decimal = 85
- binary = '0101 0101'
- elif phrase[a:b] == 'V':
- hexASCII = 43
- decimal = 86
- binary = '0101 0110'
- elif phrase[a:b] == 'W':
- hexASCII = 43
- decimal = 87
- binary = '0101 0111'
- elif phrase[a:b] == 'X':
- hexASCII = 43
- decimal = 88
- binary = '0101 1000'
- elif phrase[a:b] == 'Y':
- hexASCII = 43
- decimal = 89
- binary = '0101 1001'
- elif phrase[a:b] == 'Z':
- hexASCII = 43
- decimal = 90
- binary = '0101 1010'
- elif phrase[a:b] == 'a':
- hexASCII = 61
- decimal = 97
- binary = '0110 0001'
- elif phrase[a:b] == 'b':
- hexASCII = 62
- decimal = 98
- binary = '0110 0010'
- elif phrase[a:b] == 'c':
- hexASCII = 63
- decimal = 99
- binary = '0110 0011'
- elif phrase[a:b] == 'd':
- hexASCII = 64
- decimal = 100
- binary = '0110 0100'
- elif phrase[a:b] == 'e':
- hexASCII = 65
- decimal = 101
- binary = '0110 0101'
- elif phrase[a:b] == 'f':
- hexASCII = 66
- decimal = 102
- binary = '0110 0110'
- elif phrase[a:b] == 'g':
- hexASCII = 67
- decimal = 103
- binary = '0110 0111'
- elif phrase[a:b] == 'h':
- hexASCII = 68
- decimal = 104
- binary = '0110 1000'
- elif phrase[a:b] == 'i':
- hexASCII = 69
- decimal = 105
- binary = '0110 1001'
- elif phrase[a:b] == 'j':
- hexASCII = '6A'
- decimal = 106
- binary = '0110 1010'
- elif phrase[a:b] == 'k':
- hexASCII = '6B'
- decimal = 107
- binary = '0110 1011'
- elif phrase[a:b] == 'l':
- hexASCII = '6C'
- decimal = 108
- binary = '0110 1100'
- elif phrase[a:b] == 'm':
- hexASCII = '6D'
- decimal = 109
- binary = '0110 1101'
- elif phrase[a:b] == 'n':
- hexASCII = '6E'
- decimal = 110
- binary = '0110 1110'
- elif phrase[a:b] == 'o':
- hexASCII = '6F'
- decimal = 111
- binary = '0110 1111'
- elif phrase[a:b] == 'p':
- hexASCII = 70
- decimal = 112
- binary = '0111 0000'
- elif phrase[a:b] == 'q':
- hexASCII = 71
- decimal = 113
- binary = '0111 0001'
- elif phrase[a:b] == 'r':
- hexASCII = 72
- decimal = 114
- binary = '0111 0010'
- elif phrase[a:b] == 's':
- hexASCII = 73
- decimal = 115
- binary = '0111 0011'
- elif phrase[a:b] == 't':
- hexASCII = 74
- decimal = 116
- binary = '0111 0100'
- elif phrase[a:b] == 'u':
- hexASCII = 75
- decimal = 117
- binary = '0111 0101'
- elif phrase[a:b] == 'v':
- hexASCII = 76
- decimal = 118
- binary = '0111 0110'
- elif phrase[a:b] == 'w':
- hexASCII = 77
- decimal = 119
- binary = '0111 0111'
- elif phrase[a:b] == 'x':
- hexASCII = 78
- decimal = 120
- binary = '0111 1000'
- elif phrase[a:b] == 'y':
- hexASCII = 79
- decimal = 121
- binary = '0111 1001'
- elif phrase[a:b] == 'z':
- hexASCII = '7A'
- decimal = 122
- binary = '0111 1010'
- elif phrase[a:b] == ' ':
- hexASCII = 20
- decimal = 32
- binary = '0010 0000'
- elif phrase[a:b] == "0":
- hexASCII = 30
- decimal = 48
- binary = '0011 0000'
- elif phrase[a:b] == "1":
- hexASCII = 31
- decimal = 49
- binary = '0011 0001'
- elif phrase[a:b] == "2":
- hexASCII = 32
- decimal = 50
- binary = '0011 0010'
- elif phrase[a:b] == "3":
- hexASCII = 33
- decimal = 51
- binary = '0011 0011'
- elif phrase[a:b] == "4":
- hexASCII = 34
- decimal = 52
- binary = '0011 0100'
- elif phrase[a:b] == "5":
- hexASCII = 35
- decimal = 53
- binary = '0011 0101'
- elif phrase[a:b] == "6":
- hexASCII = 36
- decimal = 54
- binary = '0011 0110'
- elif phrase[a:b] == "7":
- hexASCII = 37
- decimal = 55
- binary = '0011 0111'
- elif phrase[a:b] == "8":
- hexASCII = 38
- decimal = 56
- binary = '0011 1000'
- elif phrase[a:b] == "9":
- hexASCII = 39
- decimal = 57
- binary = '0011 1001'
- elif phrase[a:b] == ',':
- hexASCII = "2C"
- decimal = 44
- binary = '0010 1100'
- elif phrase[a:b] == '.':
- hexASCII = "2E"
- decimal = 46
- binary = '0010 1110'
- elif phrase[a:b] == '!':
- hexASCII = 21
- decimal = 33
- binary = '0010 0001'
- else:
- error = 1
- if error != 1:
- bin.append(binary)
- print(f'Decimal value: {decimal}')
- print(f'HEX value: {hexASCII}')
- print(f'Binary Value: {binary}')
- a += 1
- b += 1
- binFull = ''.join(bin).replace(" ", "")
- binaryList.append(f'{binFull}\n')
- rerun = input('Do you want to run the program again?'
- ' Press Y for yes. Press any other key for no : ')
- with open(f'{name}.txt', 'w') as f:
- f.writelines(binaryList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement