파일을 읽어와 HEX로 출력하되 16단위로 자르기

알아두자/python 2014. 9. 13. 06:02

import sys,getopt

filename_input = "edit file name"

blocksize = 1024


opts,args = getopt.getopt(sys.argv[1:],'f:b:')

for o,a in opts:

    if o == '-f':

        filename_input = a

    if o == '-b':

        blocksize = a

    

offset = 0

with open(filename_input,"rb") as f:

    block = f.read(blocksize)

    str = ""

    count = 0

    for ch in block:

        count+=1

        if(count%16!=0):

            str += hex(ord(ch))+" "

        else:

            str += hex(ord(ch))+"\n"

        #print hex(ord(ch))

    print str