Advertisement
Retroledeom

ASCII ENCODER

May 14th, 2022
1,047
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.00 KB | None | 0 0
  1. path = 'F:\python ascii encouder'
  2.  
  3. name = input("Enter filename of test file: ")
  4. with open(f'{name}.txt', 'w') as fp:
  5.     pass
  6. rerun = 'Y'
  7.  
  8. binaryList = []
  9. while rerun.upper() == 'Y':  # runs the program on a while loop until the user tells it to stop
  10.     phrase = input("Enter your phrase: ")
  11.     count = 0  # initialization of variables for the binary to hexadecimal part
  12.     a = 0
  13.     b = 1
  14.     error = 0
  15.     bin = []
  16.     hexASCII = 0
  17.     decimal = 0
  18.     binary = 0
  19.     while error != 1:  # while loop that reads each binary number per 4 digits and converts it into a hexadecimal equivalent
  20.         count += 1
  21.         if phrase[a:b] == 'A':
  22.             hexASCII = 41
  23.             decimal = 65
  24.             binary = '0100 0001'
  25.         elif phrase[a:b] == 'B':
  26.             hexASCII = 42
  27.             decimal = 66
  28.             binary = '0100 0010'
  29.         elif phrase[a:b] == 'C':
  30.             hexASCII = 43
  31.             decimal = 67
  32.             binary = '0100 0011'
  33.         elif phrase[a:b] == 'D':
  34.             hexASCII = 44
  35.             decimal = 68
  36.             binary = '0100 0100'
  37.         elif phrase[a:b] == 'E':
  38.             hexASCII = 45
  39.             decimal = 69
  40.             binary = '0100 0101'
  41.         elif phrase[a:b] == 'F':
  42.             hexASCII = 46
  43.             decimal = 70
  44.             binary = '0100 0110'
  45.         elif phrase[a:b] == 'G':
  46.             hexASCII = 47
  47.             decimal = 71
  48.             binary = '0100 0111'
  49.         elif phrase[a:b] == 'H':
  50.             hexASCII = 48
  51.             decimal = 72
  52.             binary = '0100 1000'
  53.         elif phrase[a:b] == 'I':
  54.             hexASCII = 49
  55.             decimal = 73
  56.             binary = '0100 1001'
  57.         elif phrase[a:b] == 'J':
  58.             hexASCII = '4A'
  59.             decimal = 74
  60.             binary = '0100 1010'
  61.         elif phrase[a:b] == 'K':
  62.             hexASCII = '4B'
  63.             decimal = 75
  64.             binary = '0100 1011'
  65.         elif phrase[a:b] == 'L':
  66.             hexASCII = '4C'
  67.             decimal = 76
  68.             binary = '0100 1100'
  69.         elif phrase[a:b] == 'M':
  70.             hexASCII = '4D'
  71.             decimal = 77
  72.             binary = '0100 1101'
  73.         elif phrase[a:b] == 'N':
  74.             hexASCII = '4E'
  75.             decimal = 78
  76.             binary = '0100 1110'
  77.         elif phrase[a:b] == 'O':
  78.             hexASCII = '4F'
  79.             decimal = 79
  80.             binary = '0100 1111'
  81.         elif phrase[a:b] == 'P':
  82.             hexASCII = 43
  83.             decimal = 80
  84.             binary = '0101 0000'
  85.         elif phrase[a:b] == 'Q':
  86.             hexASCII = 43
  87.             decimal = 81
  88.             binary = '0101 0001'
  89.         elif phrase[a:b] == 'R':
  90.             hexASCII = 43
  91.             decimal = 82
  92.             binary = '0101 0010'
  93.         elif phrase[a:b] == 'S':
  94.             hexASCII = 43
  95.             decimal = 83
  96.             binary = '0101 0011'
  97.         elif phrase[a:b] == 'T':
  98.             hexASCII = 43
  99.             decimal = 84
  100.             binary = '0101 0100'
  101.         elif phrase[a:b] == 'U':
  102.             hexASCII = 43
  103.             decimal = 85
  104.             binary = '0101 0101'
  105.         elif phrase[a:b] == 'V':
  106.             hexASCII = 43
  107.             decimal = 86
  108.             binary = '0101 0110'
  109.         elif phrase[a:b] == 'W':
  110.             hexASCII = 43
  111.             decimal = 87
  112.             binary = '0101 0111'
  113.         elif phrase[a:b] == 'X':
  114.             hexASCII = 43
  115.             decimal = 88
  116.             binary = '0101 1000'
  117.         elif phrase[a:b] == 'Y':
  118.             hexASCII = 43
  119.             decimal = 89
  120.             binary = '0101 1001'
  121.         elif phrase[a:b] == 'Z':
  122.             hexASCII = 43
  123.             decimal = 90
  124.             binary = '0101 1010'
  125.         elif phrase[a:b] == 'a':
  126.             hexASCII = 61
  127.             decimal = 97
  128.             binary = '0110 0001'
  129.         elif phrase[a:b] == 'b':
  130.             hexASCII = 62
  131.             decimal = 98
  132.             binary = '0110 0010'
  133.         elif phrase[a:b] == 'c':
  134.             hexASCII = 63
  135.             decimal = 99
  136.             binary = '0110 0011'
  137.         elif phrase[a:b] == 'd':
  138.             hexASCII = 64
  139.             decimal = 100
  140.             binary = '0110 0100'
  141.         elif phrase[a:b] == 'e':
  142.             hexASCII = 65
  143.             decimal = 101
  144.             binary = '0110 0101'
  145.         elif phrase[a:b] == 'f':
  146.             hexASCII = 66
  147.             decimal = 102
  148.             binary = '0110 0110'
  149.         elif phrase[a:b] == 'g':
  150.             hexASCII = 67
  151.             decimal = 103
  152.             binary = '0110 0111'
  153.         elif phrase[a:b] == 'h':
  154.             hexASCII = 68
  155.             decimal = 104
  156.             binary = '0110 1000'
  157.         elif phrase[a:b] == 'i':
  158.             hexASCII = 69
  159.             decimal = 105
  160.             binary = '0110 1001'
  161.         elif phrase[a:b] == 'j':
  162.             hexASCII = '6A'
  163.             decimal = 106
  164.             binary = '0110 1010'
  165.         elif phrase[a:b] == 'k':
  166.             hexASCII = '6B'
  167.             decimal = 107
  168.             binary = '0110 1011'
  169.         elif phrase[a:b] == 'l':
  170.             hexASCII = '6C'
  171.             decimal = 108
  172.             binary = '0110 1100'
  173.         elif phrase[a:b] == 'm':
  174.             hexASCII = '6D'
  175.             decimal = 109
  176.             binary = '0110 1101'
  177.         elif phrase[a:b] == 'n':
  178.             hexASCII = '6E'
  179.             decimal = 110
  180.             binary = '0110 1110'
  181.         elif phrase[a:b] == 'o':
  182.             hexASCII = '6F'
  183.             decimal = 111
  184.             binary = '0110 1111'
  185.         elif phrase[a:b] == 'p':
  186.             hexASCII = 70
  187.             decimal = 112
  188.             binary = '0111 0000'
  189.         elif phrase[a:b] == 'q':
  190.             hexASCII = 71
  191.             decimal = 113
  192.             binary = '0111 0001'
  193.         elif phrase[a:b] == 'r':
  194.             hexASCII = 72
  195.             decimal = 114
  196.             binary = '0111 0010'
  197.         elif phrase[a:b] == 's':
  198.             hexASCII = 73
  199.             decimal = 115
  200.             binary = '0111 0011'
  201.         elif phrase[a:b] == 't':
  202.             hexASCII = 74
  203.             decimal = 116
  204.             binary = '0111 0100'
  205.         elif phrase[a:b] == 'u':
  206.             hexASCII = 75
  207.             decimal = 117
  208.             binary = '0111 0101'
  209.         elif phrase[a:b] == 'v':
  210.             hexASCII = 76
  211.             decimal = 118
  212.             binary = '0111 0110'
  213.         elif phrase[a:b] == 'w':
  214.             hexASCII = 77
  215.             decimal = 119
  216.             binary = '0111 0111'
  217.         elif phrase[a:b] == 'x':
  218.             hexASCII = 78
  219.             decimal = 120
  220.             binary = '0111 1000'
  221.         elif phrase[a:b] == 'y':
  222.             hexASCII = 79
  223.             decimal = 121
  224.             binary = '0111 1001'
  225.         elif phrase[a:b] == 'z':
  226.             hexASCII = '7A'
  227.             decimal = 122
  228.             binary = '0111 1010'
  229.         elif phrase[a:b] == ' ':
  230.             hexASCII = 20
  231.             decimal = 32
  232.             binary = '0010 0000'
  233.         elif phrase[a:b] == "0":
  234.             hexASCII = 30
  235.             decimal = 48
  236.             binary = '0011 0000'
  237.         elif phrase[a:b] == "1":
  238.             hexASCII = 31
  239.             decimal = 49
  240.             binary = '0011 0001'
  241.         elif phrase[a:b] == "2":
  242.             hexASCII = 32
  243.             decimal = 50
  244.             binary = '0011 0010'
  245.         elif phrase[a:b] == "3":
  246.             hexASCII = 33
  247.             decimal = 51
  248.             binary = '0011 0011'
  249.         elif phrase[a:b] == "4":
  250.             hexASCII = 34
  251.             decimal = 52
  252.             binary = '0011 0100'
  253.         elif phrase[a:b] == "5":
  254.             hexASCII = 35
  255.             decimal = 53
  256.             binary = '0011 0101'
  257.         elif phrase[a:b] == "6":
  258.             hexASCII = 36
  259.             decimal = 54
  260.             binary = '0011 0110'
  261.         elif phrase[a:b] == "7":
  262.             hexASCII = 37
  263.             decimal = 55
  264.             binary = '0011 0111'
  265.         elif phrase[a:b] == "8":
  266.             hexASCII = 38
  267.             decimal = 56
  268.             binary = '0011 1000'
  269.         elif phrase[a:b] == "9":
  270.             hexASCII = 39
  271.             decimal = 57
  272.             binary = '0011 1001'
  273.         elif phrase[a:b] == ',':
  274.             hexASCII = "2C"
  275.             decimal = 44
  276.             binary = '0010 1100'
  277.         elif phrase[a:b] == '.':
  278.             hexASCII = "2E"
  279.             decimal = 46
  280.             binary = '0010 1110'
  281.         elif phrase[a:b] == '!':
  282.             hexASCII = 21
  283.             decimal = 33
  284.             binary = '0010 0001'
  285.         else:
  286.             error = 1
  287.         if error != 1:
  288.             bin.append(binary)
  289.             print(f'Decimal value: {decimal}')
  290.             print(f'HEX value: {hexASCII}')
  291.             print(f'Binary Value: {binary}')
  292.  
  293.         a += 1
  294.         b += 1
  295.     binFull = ''.join(bin).replace(" ", "")
  296.     binaryList.append(f'{binFull}\n')
  297.     rerun = input('Do you want to run the program again?'
  298.                   ' Press Y for yes. Press any other key for no : ')
  299.  
  300. with open(f'{name}.txt', 'w') as f:
  301.     f.writelines(binaryList)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement