import sys import binascii import struct if len(sys.argv) != 2: print "%s <input file>" % sys.argv[0] exit() print "Input file: %s" % sys.argv[1] fdInput = open(sys.argv[1], 'rb') ### HEX ### print "HEX" i = 0 while 1: b = fdInput.read(1) if b == "": break print binascii.b2a_hex(b), if ((i + 1) % 16) == 0: print "" # Line break i = i + 1 print "" print "" ### Character ### fdInput.seek(0) print "Character" while 1: b = fdInput.read(1) if b == "": break print "%c" % b, print "" print "" ### 16 byte String data ### fdInput.seek(0) b = fdInput.read(16) print "%s" % b print "" ### 4 byte unsigned char data ### i = 0 while 1: if i >= 4: break i = i + 1 b = fdInput.read(1) print binascii.b2a_hex(b), print "" print "" ### 2 byte unsigned short data ### b = fdInput.read(2) s = struct.unpack('!H', b) # Network byte order (Big endian) print "0x%04x" % s, print " = %d" % s print "" ### 4 byte unsigned long data ### b = fdInput.read(4) l = struct.unpack('!L', b) print "0x%08x" % l, print " = %d" % l fdInput.close()
実行結果
> python binFile.py sample.bin Input file: sample.bin HEX 48 45 4c 4c 4f 00 00 00 00 00 00 00 00 00 00 00 01 02 03 04 01 f4 00 01 5f 90 Character H E L L O HELLO 01 02 03 04 0x01f4 = 500 0x00015f90 = 90000
0 件のコメント:
コメントを投稿