1 |
#!/usr/bin/python |
2 |
|
3 |
import getopt |
4 |
import sys |
5 |
import string |
6 |
import struct |
7 |
|
8 |
def txt2bin(infn, outfn, offset=0): |
9 |
infile = sys.stdin |
10 |
outfile = sys.stdout |
11 |
|
12 |
if '-' != infn: |
13 |
infile = open(infn, 'r') |
14 |
|
15 |
if '-' != outfn: |
16 |
outfile = open(outfn, 'w') |
17 |
|
18 |
bytes = infile.read().replace("\n", ' ').strip().split(' ') |
19 |
for t in bytes[offset:]: |
20 |
if '' == t: |
21 |
continue |
22 |
outfile.write(struct.pack("B", string.atoi(t, 16))) |
23 |
|
24 |
infile.close() |
25 |
outfile.close() |
26 |
|
27 |
|
28 |
if '__main__' == __name__: |
29 |
offset = 0 |
30 |
optlist, args = getopt.getopt(sys.argv[1:], 'o:') |
31 |
if len(args) < 2: |
32 |
print "Usage: %s [-t offset] <infile> <outfile>" % sys.argv[0] |
33 |
sys.exit(1) |
34 |
|
35 |
for opt, arg in optlist: |
36 |
if '-o' == opt: |
37 |
offset = int(arg) |
38 |
else: |
39 |
print "Unrecognized option %s" % opt |
40 |
sys.exit(1) |
41 |
|
42 |
txt2bin(args[0], args[1], offset=offset) |
43 |
|
44 |
|
45 |
|
46 |
|