summaryrefslogtreecommitdiffstats
path: root/test_20121121/hex2binary.py
blob: a4002daffc97839926c7896dbfef073692b91974 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python

import sys

def main(fin, fout):
    last = None

    for c in fin.read():
        if c not in "0123456789ABCDEF":
            continue
        if last == None:
            last = c
            continue
        fout.write(chr(int("0x" + last + c, 16)))
        last = None


if __name__=="__main__":
    if len(sys.argv) == 1:
        fin = sys.stdin
        fout = sys.stdout
    elif len(sys.argv) == 3:
        fin = open(sys.argv[1], 'r')
        fout = open(sys.argv[2], 'w')
        print "working..."
    else:
        print "ERROR: wrong number of arguments (in_file + out_file OR none)"

    main(fin, fout)
    fin.close()
    fout.close()