Advertisement
Retroledeom

ASCII DECODER

May 5th, 2022
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.63 KB | None | 0 0
  1. binlist = ['0000',
  2.            '0001',
  3.            '0010',
  4.            '0011',
  5.            '0100',
  6.            '0101',
  7.            '0110',
  8.            '0111',
  9.            '1000',
  10.            '1001',
  11.            '1010',
  12.            '1011',
  13.            '1100',
  14.            '1101',
  15.            '1110',
  16.            '1111']
  17. hexnum = "0123456789ABCDEF"
  18. CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  19. char = "abcdefghijklmnopqrstuvwxyz"
  20. num = '0123456789'
  21. rerun = 'Y'
  22. while rerun.upper() == 'Y':  # runs the program on a while loop until the user tells it to stop
  23.     name = input("Enter filename of test file: ")  # where the user will input the name of the file
  24.     with open(f'{name}.txt', 'r') as file:  # opens the file inputted by the user
  25.         data = file.read().replace('\n', '')  # converts the textfile into string and removes the newlines
  26.     print(f'binary: {data}')  # first prints out whatever is on the file
  27.     hex = []
  28.     count = 0  # initialization of variables for the binary to hexadecimal part
  29.     a = 0
  30.     b = 4
  31.     error = 0
  32.     while error != 1:  # while loop that reads each binary number per 4 digits and converts it into a hexadecimal equivalent
  33.         count += 1
  34.         if data[a:b] == binlist[1]:
  35.             hex.append(hexnum[1])
  36.         elif data[a:b] == binlist[2]:
  37.             hex.append(hexnum[2])
  38.         elif data[a:b] == binlist[3]:
  39.             hex.append(hexnum[3])
  40.         elif data[a:b] == binlist[4]:
  41.             hex.append(hexnum[4])
  42.         elif data[a:b] == binlist[5]:
  43.             hex.append(hexnum[5])
  44.         elif data[a:b] == binlist[6]:
  45.             hex.append(hexnum[6])
  46.         elif data[a:b] == binlist[7]:
  47.             hex.append(hexnum[7])
  48.         elif data[a:b] == binlist[8]:
  49.             hex.append(hexnum[8])
  50.         elif data[a:b] == binlist[9]:
  51.             hex.append(hexnum[9])
  52.         elif data[a:b] == binlist[10]:
  53.             hex.append(hexnum[10])
  54.         elif data[a:b] == binlist[11]:
  55.             hex.append(hexnum[11])
  56.         elif data[a:b] == binlist[12]:
  57.             hex.append(hexnum[12])
  58.         elif data[a:b] == binlist[13]:
  59.             hex.append(hexnum[13])
  60.         elif data[a:b] == binlist[14]:
  61.             hex.append(hexnum[14])
  62.         elif data[a:b] == binlist[15]:
  63.             hex.append(hexnum[15])
  64.         elif data[a:b] == binlist[0]:
  65.             hex.append(hexnum[0])
  66.         else:
  67.             error = 1
  68.         a += 4
  69.         b += 4
  70.     c = 0  # initialization of variables for the hexadecimal to character part
  71.     d = 2
  72.     error2 = 0
  73.     hexanum = "".join(hex)  # joins the hexadecimal array into a single string
  74.     print(f'hex number: {hexanum}')
  75.     text = []
  76.     hexanum = str(hexanum)
  77.     while error2 != 1:  # while loop that reads each hexadecimal by 2 digits and converts that into a character
  78.         count += 1
  79.         if hexanum[c:d] == '30':
  80.             text.append(num[0])
  81.         elif hexanum[c:d] == '20':
  82.             text.append(' ')
  83.         elif hexanum[c:d] == '31':
  84.             text.append(num[1])
  85.         elif hexanum[c:d] == '32':
  86.             text.append(num[2])
  87.         elif hexanum[c:d] == '33':
  88.             text.append(num[3])
  89.         elif hexanum[c:d] == '34':
  90.             text.append(num[4])
  91.         elif hexanum[c:d] == '35':
  92.             text.append(num[5])
  93.         elif hexanum[c:d] == '36':
  94.             text.append(num[6])
  95.         elif hexanum[c:d] == '37':
  96.             text.append(num[7])
  97.         elif hexanum[c:d] == '38':
  98.             text.append(num[8])
  99.         elif hexanum[c:d] == '39':
  100.             text.append(num[9])
  101.         elif hexanum[c:d] == '41':
  102.             text.append(CHAR[0])
  103.         elif hexanum[c:d] == '42':
  104.             text.append(CHAR[1])
  105.         elif hexanum[c:d] == '43':
  106.             text.append(CHAR[2])
  107.         elif hexanum[c:d] == '44':
  108.             text.append(CHAR[3])
  109.         elif hexanum[c:d] == '45':
  110.             text.append(CHAR[4])
  111.         elif hexanum[c:d] == '46':
  112.             text.append(CHAR[5])
  113.         elif hexanum[c:d] == '47':
  114.             text.append(CHAR[6])
  115.         elif hexanum[c:d] == '48':
  116.             text.append(CHAR[7])
  117.         elif hexanum[c:d] == '49':
  118.             text.append(CHAR[8])
  119.         elif hexanum[c:d] == '4A':
  120.             text.append(CHAR[9])
  121.         elif hexanum[c:d] == '4B':
  122.             text.append(CHAR[10])
  123.         elif hexanum[c:d] == '4C':
  124.             text.append(CHAR[11])
  125.         elif hexanum[c:d] == '4D':
  126.             text.append(CHAR[12])
  127.         elif hexanum[c:d] == '4E':
  128.             text.append(CHAR[13])
  129.         elif hexanum[c:d] == '4F':
  130.             text.append(CHAR[14])
  131.         elif hexanum[c:d] == '50':
  132.             text.append(CHAR[15])
  133.         elif hexanum[c:d] == '51':
  134.             text.append(CHAR[16])
  135.         elif hexanum[c:d] == '52':
  136.             text.append(CHAR[17])
  137.         elif hexanum[c:d] == '53':
  138.             text.append(CHAR[18])
  139.         elif hexanum[c:d] == '54':
  140.             text.append(CHAR[19])
  141.         elif hexanum[c:d] == '55':
  142.             text.append(CHAR[20])
  143.         elif hexanum[c:d] == '56':
  144.             text.append(CHAR[21])
  145.         elif hexanum[c:d] == '57':
  146.             text.append(CHAR[22])
  147.         elif hexanum[c:d] == '58':
  148.             text.append(CHAR[23])
  149.         elif hexanum[c:d] == '59':
  150.             text.append(CHAR[24])
  151.         elif hexanum[c:d] == '5A':
  152.             text.append(CHAR[25])
  153.         elif hexanum[c:d] == '61':
  154.             text.append(char[0])
  155.         elif hexanum[c:d] == '62':
  156.             text.append(char[1])
  157.         elif hexanum[c:d] == '63':
  158.             text.append(char[2])
  159.         elif hexanum[c:d] == '64':
  160.             text.append(char[3])
  161.         elif hexanum[c:d] == '65':
  162.             text.append(char[4])
  163.         elif hexanum[c:d] == '66':
  164.             text.append(char[5])
  165.         elif hexanum[c:d] == '67':
  166.             text.append(char[6])
  167.         elif hexanum[c:d] == '68':
  168.             text.append(char[7])
  169.         elif hexanum[c:d] == '69':
  170.             text.append(char[8])
  171.         elif hexanum[c:d] == '6A':
  172.             text.append(char[9])
  173.         elif hexanum[c:d] == '6B':
  174.             text.append(char[10])
  175.         elif hexanum[c:d] == '6C':
  176.             text.append(char[11])
  177.         elif hexanum[c:d] == '6D':
  178.             text.append(char[12])
  179.         elif hexanum[c:d] == '6E':
  180.             text.append(char[13])
  181.         elif hexanum[c:d] == '6F':
  182.             text.append(char[14])
  183.         elif hexanum[c:d] == '70':
  184.             text.append(char[15])
  185.         elif hexanum[c:d] == '71':
  186.             text.append(char[16])
  187.         elif hexanum[c:d] == '72':
  188.             text.append(char[17])
  189.         elif hexanum[c:d] == '73':
  190.             text.append(char[18])
  191.         elif hexanum[c:d] == '73':
  192.             text.append(char[19])
  193.         elif hexanum[c:d] == '74':
  194.             text.append(char[19])
  195.         elif hexanum[c:d] == '75':
  196.             text.append(char[20])
  197.         elif hexanum[c:d] == '76':
  198.             text.append(char[21])
  199.         elif hexanum[c:d] == '77':
  200.             text.append(char[22])
  201.         elif hexanum[c:d] == '78':
  202.             text.append(char[23])
  203.         elif hexanum[c:d] == '79':
  204.             text.append(char[24])
  205.         elif hexanum[c:d] == '7A':
  206.             text.append(char[25])
  207.         elif hexanum[c:d] == '3B':
  208.             text.append(';')
  209.         else:
  210.             error2 = 1
  211.         c += 2
  212.         d += 2
  213.     text = "".join(text)  # joins the character array into a single string
  214.     print('')
  215.     print(f'Converted ASCII: {text}')  # prints out the final converted text
  216.     print('')
  217.     rerun = input('Do you want to run the program again?'
  218.                   ' Press Y for yes. Press any other key for no : ')
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement