Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- binlist = ['0000',
- '0001',
- '0010',
- '0011',
- '0100',
- '0101',
- '0110',
- '0111',
- '1000',
- '1001',
- '1010',
- '1011',
- '1100',
- '1101',
- '1110',
- '1111']
- hexnum = "0123456789ABCDEF"
- CHAR = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- char = "abcdefghijklmnopqrstuvwxyz"
- num = '0123456789'
- rerun = 'Y'
- while rerun.upper() == 'Y': # runs the program on a while loop until the user tells it to stop
- name = input("Enter filename of test file: ") # where the user will input the name of the file
- with open(f'{name}.txt', 'r') as file: # opens the file inputted by the user
- data = file.read().replace('\n', '') # converts the textfile into string and removes the newlines
- print(f'binary: {data}') # first prints out whatever is on the file
- hex = []
- count = 0 # initialization of variables for the binary to hexadecimal part
- a = 0
- b = 4
- error = 0
- while error != 1: # while loop that reads each binary number per 4 digits and converts it into a hexadecimal equivalent
- count += 1
- if data[a:b] == binlist[1]:
- hex.append(hexnum[1])
- elif data[a:b] == binlist[2]:
- hex.append(hexnum[2])
- elif data[a:b] == binlist[3]:
- hex.append(hexnum[3])
- elif data[a:b] == binlist[4]:
- hex.append(hexnum[4])
- elif data[a:b] == binlist[5]:
- hex.append(hexnum[5])
- elif data[a:b] == binlist[6]:
- hex.append(hexnum[6])
- elif data[a:b] == binlist[7]:
- hex.append(hexnum[7])
- elif data[a:b] == binlist[8]:
- hex.append(hexnum[8])
- elif data[a:b] == binlist[9]:
- hex.append(hexnum[9])
- elif data[a:b] == binlist[10]:
- hex.append(hexnum[10])
- elif data[a:b] == binlist[11]:
- hex.append(hexnum[11])
- elif data[a:b] == binlist[12]:
- hex.append(hexnum[12])
- elif data[a:b] == binlist[13]:
- hex.append(hexnum[13])
- elif data[a:b] == binlist[14]:
- hex.append(hexnum[14])
- elif data[a:b] == binlist[15]:
- hex.append(hexnum[15])
- elif data[a:b] == binlist[0]:
- hex.append(hexnum[0])
- else:
- error = 1
- a += 4
- b += 4
- c = 0 # initialization of variables for the hexadecimal to character part
- d = 2
- error2 = 0
- hexanum = "".join(hex) # joins the hexadecimal array into a single string
- print(f'hex number: {hexanum}')
- text = []
- hexanum = str(hexanum)
- while error2 != 1: # while loop that reads each hexadecimal by 2 digits and converts that into a character
- count += 1
- if hexanum[c:d] == '30':
- text.append(num[0])
- elif hexanum[c:d] == '20':
- text.append(' ')
- elif hexanum[c:d] == '31':
- text.append(num[1])
- elif hexanum[c:d] == '32':
- text.append(num[2])
- elif hexanum[c:d] == '33':
- text.append(num[3])
- elif hexanum[c:d] == '34':
- text.append(num[4])
- elif hexanum[c:d] == '35':
- text.append(num[5])
- elif hexanum[c:d] == '36':
- text.append(num[6])
- elif hexanum[c:d] == '37':
- text.append(num[7])
- elif hexanum[c:d] == '38':
- text.append(num[8])
- elif hexanum[c:d] == '39':
- text.append(num[9])
- elif hexanum[c:d] == '41':
- text.append(CHAR[0])
- elif hexanum[c:d] == '42':
- text.append(CHAR[1])
- elif hexanum[c:d] == '43':
- text.append(CHAR[2])
- elif hexanum[c:d] == '44':
- text.append(CHAR[3])
- elif hexanum[c:d] == '45':
- text.append(CHAR[4])
- elif hexanum[c:d] == '46':
- text.append(CHAR[5])
- elif hexanum[c:d] == '47':
- text.append(CHAR[6])
- elif hexanum[c:d] == '48':
- text.append(CHAR[7])
- elif hexanum[c:d] == '49':
- text.append(CHAR[8])
- elif hexanum[c:d] == '4A':
- text.append(CHAR[9])
- elif hexanum[c:d] == '4B':
- text.append(CHAR[10])
- elif hexanum[c:d] == '4C':
- text.append(CHAR[11])
- elif hexanum[c:d] == '4D':
- text.append(CHAR[12])
- elif hexanum[c:d] == '4E':
- text.append(CHAR[13])
- elif hexanum[c:d] == '4F':
- text.append(CHAR[14])
- elif hexanum[c:d] == '50':
- text.append(CHAR[15])
- elif hexanum[c:d] == '51':
- text.append(CHAR[16])
- elif hexanum[c:d] == '52':
- text.append(CHAR[17])
- elif hexanum[c:d] == '53':
- text.append(CHAR[18])
- elif hexanum[c:d] == '54':
- text.append(CHAR[19])
- elif hexanum[c:d] == '55':
- text.append(CHAR[20])
- elif hexanum[c:d] == '56':
- text.append(CHAR[21])
- elif hexanum[c:d] == '57':
- text.append(CHAR[22])
- elif hexanum[c:d] == '58':
- text.append(CHAR[23])
- elif hexanum[c:d] == '59':
- text.append(CHAR[24])
- elif hexanum[c:d] == '5A':
- text.append(CHAR[25])
- elif hexanum[c:d] == '61':
- text.append(char[0])
- elif hexanum[c:d] == '62':
- text.append(char[1])
- elif hexanum[c:d] == '63':
- text.append(char[2])
- elif hexanum[c:d] == '64':
- text.append(char[3])
- elif hexanum[c:d] == '65':
- text.append(char[4])
- elif hexanum[c:d] == '66':
- text.append(char[5])
- elif hexanum[c:d] == '67':
- text.append(char[6])
- elif hexanum[c:d] == '68':
- text.append(char[7])
- elif hexanum[c:d] == '69':
- text.append(char[8])
- elif hexanum[c:d] == '6A':
- text.append(char[9])
- elif hexanum[c:d] == '6B':
- text.append(char[10])
- elif hexanum[c:d] == '6C':
- text.append(char[11])
- elif hexanum[c:d] == '6D':
- text.append(char[12])
- elif hexanum[c:d] == '6E':
- text.append(char[13])
- elif hexanum[c:d] == '6F':
- text.append(char[14])
- elif hexanum[c:d] == '70':
- text.append(char[15])
- elif hexanum[c:d] == '71':
- text.append(char[16])
- elif hexanum[c:d] == '72':
- text.append(char[17])
- elif hexanum[c:d] == '73':
- text.append(char[18])
- elif hexanum[c:d] == '73':
- text.append(char[19])
- elif hexanum[c:d] == '74':
- text.append(char[19])
- elif hexanum[c:d] == '75':
- text.append(char[20])
- elif hexanum[c:d] == '76':
- text.append(char[21])
- elif hexanum[c:d] == '77':
- text.append(char[22])
- elif hexanum[c:d] == '78':
- text.append(char[23])
- elif hexanum[c:d] == '79':
- text.append(char[24])
- elif hexanum[c:d] == '7A':
- text.append(char[25])
- elif hexanum[c:d] == '3B':
- text.append(';')
- else:
- error2 = 1
- c += 2
- d += 2
- text = "".join(text) # joins the character array into a single string
- print('')
- print(f'Converted ASCII: {text}') # prints out the final converted text
- print('')
- rerun = input('Do you want to run the program again?'
- ' Press Y for yes. Press any other key for no : ')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement