#!/usr/bin/python
import sys

def checkCommandLine(bin_bool, bout_bool):
    std_input = []
    args = ''
    for arg in sys.argv[1:]:
        if arg == '-xin':
            bin_bool = False
        elif arg == '-bout':
            bout_bool = True
        elif arg == '-bin' or arg == '-xout':
            pass #binary input and hexadecimal output are default
        else:
            args += arg
    if not sys.stdin.isatty():
        for line in sys.stdin.read():
            args += line
    if bin_bool:
        if all(c in ['0', '1'] for c in list(args.replace(' ', ''))):
            std_input = [args[i:i+4] for i in range(0, len(args), 4)]
        else:
            args = [ord(i) for i in list(args)]
            for b in args:
                std_input.append((b >> 4))
                std_input.append(b & 15)
    else:
        args = args.replace('\n', '')
        std_input = list(args.replace(' ', ''))
    checkStandardInput(bin_bool, bout_bool, std_input)

def checkStandardInput(bin_bool, bout_bool, std_input):
    if not bin_bool:
        for i in std_input:
            if i not in ['0', '1', '2', '3', '4', '5',
                         '6', '7', '8', '9', 'a', 'b',
                         'c', 'd', 'e', 'f', 'A', 'B',
                         'C', 'D', 'E', 'F']:
                raise ValueError('{} is not a valid hexadecimal character'.format(i))
    if bout_bool:
        toBinary(bin_bool, std_input)
    else:
        toHex(bin_bool, std_input)
    
def toBinary(bin_bool, std_input):
    hex_bin_table = {'0':'0000', '1':'0001', '2':'0010', '3':'0011',
                     '4':'0100', '5':'0101', '6':'0110', '7':'0111',
                     '8':'1000', '9':'1001', 'A':'1010', 'B':'1011',
                     'C':'1100', 'D':'1101', 'E':'1110', 'F':'1111'}
    x = 0
    chr_str = 0
    for hexas in std_input:
        if x == 2:
            sys.stdout.write(chr(chr_str))
            chr_str = 0
            x = 1
        else:
            x += 1
        if not bin_bool:
            hexas = hexas.upper()
            chr_str = chr_str << 4
            chr_str = chr_str | int(hex_bin_table[hexas], 2)
        else:
            chr_str = chr_str << 4
            chr_str = chr_str | hexas
    sys.stdout.write(chr(chr_str))

def toHex(bin_bool, std_input):
    bin_hex_table = {'0000':'0', '0001':'1', '0010':'2', '0011':'3',
                     '0100':'4', '0101':'5', '0110':'6', '0111':'7',
                     '1000':'8', '1001':'9', '1010':'A', '1011':'B',
                     '1100':'C', '1101':'D', '1110':'E', '1111':'F'}
    x = 0
    y = 0
    output_str = ''
    for bins in std_input:
        if x == 2:
            output_str += ' '
            x = 1
        else:
            x += 1
        if y == 24:
            output_str += '\n'
            y = 1
        else:
            y += 1
        if bin_bool:
            output_str += bin_hex_table[format(bins, '04b')]
        else:
            output_str += bins
    print output_str
    
if __name__ == "__main__" :
    bin_bool = True
    bout_bool = False
    checkCommandLine(bin_bool, bout_bool)
