# # convert ASCII representation of bytes to bytes # each pair of characters input represents a byte # all white space ignored # import sys import string # # quit on something bad # with an error message and exit code # # str = error message to be printed # def goodbye(str): print(str) sys.exit(1) # # convert character representation of hex digit to a hex digit # return - if you're given a non-hex digit # def cvthex(c): return(string.hexdigits.find(c)) # ############################################# # MAIN ROUTINE # # line number of input for error purposes # lineno = 0 # # loop until EOF, then quit # while True: # # read in a line # not error handling # try: # read something line = input('') except EOFError: # nothing more to read break except: # something else happened goodbye("Could not read input") # # now process the line # should be of the form xx xx xx xx . . . # where xx are 2 hex digits; bomb if not # # at a new line lineno += 1 # break the line up into pairs listch = line.split(' ') # process each pair for i in listch: if len(i) != 2: # not a pair -- complain and stop goodbye("line {0}: sequence {1} not 2 long".format(lineno, i)) else: # convert each char to a hex value c0 = cvthex(i[0]) c1 = cvthex(i[1]) # complain if given a non-hex digit char if c0 == -1 or c1 == -1: goodbye("line {0}: non-hex digit in {1}".format(lineno, i)) # its good -- create the corresponding byte else: actualch = c0 * 16 + c1 # now write it to the output print("{0:c}".format(actualch), end='')