Coding/Python 삽질기2008. 5. 19. 00:42
#-*- coding: cp949 -*-
# @Author : chobocho
# @Title  : Simple Hex viewer
# @Date   : 2008. 5. 18
import sys
def main(source_file):
   
    source = open(source_file, 'rb')
    source_data = source.read()
    source.close()
   
    char_count = 0
    buffer = ""
   
    for ch in source_data:
        if (char_count % 16 == 0):
            print "|  %s\n%05X " % (buffer, char_count),
            buffer = ""
        if (char_count % 4 == 0):
            print " ",
        print "%02X" % int( ord(ch) ),
       
        if (ch == '\r'):
            buffer += ""
        elif (ch == '\n'):
            buffer += '\\n'
        elif (ch == '\t'):
            buffer += '\\t'
        else :
            buffer += ch
       
        char_count += 1
       
  
   
#-------------------------------------------------------------------------------
# main
if __name__ == "__main__": 
    if len(sys.argv) < 2:
        print "Usage : hex target"
    else:
        main(sys.argv[1])

'Coding > Python 삽질기' 카테고리의 다른 글

Fractal Tree  (0) 2008.06.21
Post it 만들기  (0) 2008.03.04
Python으로 만든 file copy 예제  (2) 2007.11.20
Posted by chobocho