#!/usr/bin/env python

import optparse
import struct
import sys


def hex_to_raw_bytes(hexdata):
  bytes = []
  for piece in hexdata.split():
    print piece
    part = struct.pack("B", int(piece, base=16))
    bytes.append(part)

  return ''.join(bytes)

def main(argv):
  parser = optparse.OptionParser(usage="usage: %prog <infile> <outfile>")

  options, args = parser.parse_args(argv)
  if len(args) != 3:
    parser.error("incorrect number of arguments")

  infile = open(args[1])
  outfile = open(args[2], 'w')

  outfile.write(hex_to_raw_bytes(infile.read()))

  infile.close()
  outfile.close()

if __name__ == "__main__":
  sys.exit(main(sys.argv))

