1+ #49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d
2+ #SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t
3+ ########https://base64.guru/converter/encode/hex;
4+ hexalph = """!"#$%&'()*+,-./0123456789:'<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""
5+ hexdecalph = '0123456789abcdef'
6+ finalascii = """ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"""
7+ fname = 'NEWtestoutput.txt'
8+ fhand = open (fname , 'w' )
9+ fhand .close ()
10+ fhand = open (fname , 'r+' )
11+ #
12+ #
13+ #
14+ #below starts the function
15+ def hextodec (hexstring ) :
16+ #initialize empty list to contain hex pairs
17+ hexlist = []
18+ #turn the input string into a list
19+ hexinput = list (hexstring )
20+ #loop through the list of characters and pair them up
21+ while len (hexinput ) > 0 :
22+ #take first two characters, turn them into a string, assign them to a variable
23+ hexpair = '' .join (hexinput [:2 ])
24+ fhand .write (f'Parsed hex pair { hexpair } \n ' )
25+ #add the hex pair to the list
26+ hexlist .append (hexpair )
27+ #remove the first two characters from the original input string list
28+ hexinput = hexinput [2 :]
29+ fhand .write (f'List of parsed hex pairs is { hexlist } \n ' )
30+ declist = []
31+ #going through each pair, organizing, and converting to decimal
32+ for pair in hexlist :
33+ #turn the pair into a list and change to lowercase to match hex character list above
34+ hextodecinput = list (pair .lower ())
35+ fhand .write (f'Lowercase pair list is { hextodecinput } \n ' )
36+ #reverse the list to process from lowest priority to highest
37+ hextodecinput .reverse ()
38+ fhand .write (f'Reversed list is { hextodecinput } \n ' )
39+ total = 0
40+ #loop through each character and convert from hexadecimal to decimal
41+ for char in hextodecinput :
42+ fhand .write (f'{ hexdecalph .index (char )} is index of character in hexdecalph list\n ' )
43+ fhand .write (f'{ hextodecinput .index (char )} is index of character in input\n ' )
44+ fhand .write (f'{ 16 ** hextodecinput .index (char )} is 16 to the power of the index of the character in the input\n ' )
45+ fhand .write (f'---Equation will be { hexdecalph .index (char )} * { 16 ** hextodecinput .index (char )} \n ' )
46+ #do the actual hexadecimal conversion
47+ total += ((hexdecalph .index (char )) * (16 ** hextodecinput .index (char )))
48+ fhand .write (f'---Running total is { total } \n ' )
49+ #turn total into a string and append to list
50+ total = str (total )
51+ fhand .write (f'{ total } is decimal conversion\n ' )
52+ declist .append (total )
53+ fhand .write (f'{ declist } is list of decimal conversions\n ' )
54+ binlist = []
55+ #loop through each decimal in the list to convert to final base64 ASCII characters
56+ for dec in declist :
57+ #convert to integer
58+ dec2 = int (dec )
59+ #convert to binary, padding with leading zeros if necessary for 8 total characters
60+ decbin = f'{ dec2 :08b} '
61+ decbin = list (decbin )
62+ decbin = '' .join (decbin )
63+ fhand .write (f'{ decbin } is binary conversion of decimal\n ' )
64+ binlist .append (decbin )
65+ binlist = '' .join (binlist )
66+ #to convert to base64, 6bit words are needed. this ensures the list is divisible by 6
67+ if not len (binlist ) % 6 == 0 :
68+ binlist = list (binlist )
69+ binlist .append ('00' )
70+ binlist = '' .join (binlist )
71+ if not len (binlist ) % 6 == 0 :
72+ binlist = list (binlist )
73+ binlist .append ('00' )
74+ binlist = '' .join (binlist )
75+ sixbitlist = []
76+ #loop through the list, separating bits out into words of 6
77+ while len (binlist ) > 0 :
78+ binword = binlist [:6 ]
79+ binlist = binlist [6 :]
80+ binword = '' .join (binword )
81+ fhand .write (f'Parsed 6-bit word { binword } \n ' )
82+ sixbitlist .append (binword )
83+ finaldeclist = []
84+ #loop through each 6-bit word in list, converting to decimal
85+ for item in sixbitlist :
86+ #convert the word to integer in base2
87+ newdec = int (item , 2 )
88+ newdec = str (newdec )
89+ fhand .write (f'{ newdec } is decimal conversion of 6-bit word { item } \n ' )
90+ finaldeclist .append (newdec )
91+ finalcharlist = []
92+ #loop through list of decimal conversions, converting to ASCII using the base64 conversion table
93+ for item in finaldeclist :
94+ finalchar = int (item )
95+ finalchar = finalascii [finalchar ]
96+ finalchar = str (finalchar )
97+ fhand .write (f'{ item } is decimal in list to convert using base64 table\n ' )
98+ fhand .write (f'{ finalchar } is final character in base64 list using decimal conversion of 6-bit binary word as index\n ' )
99+ finalcharlist .append (finalchar )
100+ finalword = '' .join (finalcharlist )
101+ finalword = list (finalword )
102+ #base64 strings are divisible by 4, so the following three lines ensure that the string is padded with ending '=' if necessary
103+ if not len (finalword ) % 4 == 0 :
104+ finalword .append ('=' )
105+ if not len (finalword ) % 4 == 0 :
106+ finalword .append ('=' )
107+ if not len (finalword ) % 4 == 0 :
108+ finalword .append ('=' )
109+ finalword = '' .join (finalword )
110+ fhand .write (f'{ finalword } is base64 conversion of { hexstring } \n ' )
111+ return finalword
112+ print (hextodec (input ('' )))
0 commit comments