Python 언어 소스:

# Filename: testHexView_03.py
#
# Execute: python testHexView_03.py [filename]
# Execute: ipy testHexView_03.py [filename]

import os
import sys

def toHex(b):
    s = ""
    x1 = (b & 0xF0) >> 4
    x2 = b & 0x0F
    if x1 < 10:
        s += chr(x1 + ord('0'[0]))
    else:
     s += chr((x1 - 10) + ord('A'))
    if x2 < 10:
        s += chr(x2 + ord('0'))
    else:
        s += chr((x2 - 10) + ord('A'))
    return s

def toHex8(n):
    s = ""
    x1 = (n & 0xF0000000) >> 28
    x2 = (n & 0xF000000) >> 24
    x3 = (n & 0xF00000) >> 20
    x4 = (n & 0xF0000) >> 16
    x5 = (n & 0xF000) >> 12
    x6 = (n & 0xF0) >> 8
    x7 = (n & 0xF0) >> 4
    x8 = n & 0x0F
    if x1 < 10:
        s += chr(x1 + ord('0'))
    else:
        s += chr((x1 - 10) + ord('A'))
    if x2 < 10:
        s += chr(x2 + ord('0'))
    else:
        s += chr((x2 - 10) + ord('A'))
    if x3 < 10:
        s += chr(x3 + ord('0'))
    else:
        s += chr((x3 - 10) + ord('A'))
    if x4 < 10:
        s += chr(x4 + ord('0'))
    else:
        s += chr((x4 - 10) + ord('A'))
    s += " "
    if x5 < 10:
        s += chr(x5 + ord('0'))
    else:
        s += chr((x5 - 10) + ord('A'))
    if x6 < 10:
        s += chr(x6 + ord('0'))
    else:
        s += chr((x6 - 10) + ord('A'))
    if x7 < 10:
        s += chr(x7 + ord('0'))
    else:
        s += chr((x7 - 10) + ord('A'))
    if x8 < 10:
        s += chr(x8 + ord('0'))
    else:
        s += chr((x8 - 10) + ord('A'))
  
    return s

 

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print "Usage: python testHexView_03.py [filename]"
        sys.exit(1)

    fname = sys.argv[1]

    if not os.path.exists(fname):
        print "The file '%s' does not exist.\n" % fname
        sys.exit(1)
    elif os.path.isdir(fname):
        print "%s is a directory.\n" % fname
        sys.exit(1)

    fsize = os.path.getsize(fname)

    if fsize != None:
        print "The size of the file \"%s\" is %d." % (fname, fsize)
        if fsize == 0:
            sys.exit(1)
    print

    try:
        file2 = open(fname, "rb")
        n = 0
        dum = ""
        for i in range(fsize):
            if n % 16 == 0:
                sys.stdout.write("%s: " % toHex8(n & 0xFFFFFFFF))
             
            if n % 16 != 8:
                sys.stdout.write(" ")
            else:
                sys.stdout.write("-")

            a = file2.read(1)
            sys.stdout.write("%s" % toHex(int(ord(a))))
               
            if int(ord(a)) < int(ord(' ')) or int(ord(a)) & 0x80 != 0:
                dum += "."
            else:
                dum += a
               
            if n > 0 and n % 16 == 15:
                sys.stdout.write("  |%s|\n" % dum)
                dum = ""

            n += 1

        if n > 0 and n % 16 > 0:
            for i in range(16 - (fsize % 16)):
                sys.stdout.write("   ")
            sys.stdout.write("  |%s" %dum)
            for i in range(16 - (fsize % 16)):
                sys.stdout.write(" ")
            sys.stdout.write("|\n")
            dum = ""
    except IOError: 
        print "Error: the file \"%s\" cannot be read more." % fname
    finally:
        file2.close()
        print
        print "Read %d bytes\n" % n

 

 

예 1
Python으로 실행하기> python testHexView_03.py temp_1.bin
IronPython으로 실행하기> ipy testHexView_03.py temp_1.bin
Jython으로 실행하기> jython testHexView_03.py temp_1.bin
The size of the file "temp_1.bin" is 12.

0000 0000:  48 65 6C 6C 6F 20 74 68-65 72 65 0A              |Hello there.    |

Read 12 bytes

 

예 2
Python으로 실행하기> python testHexView_03.py myFile.ser
IronPython으로 실행하기> ipy testHexView_03.py myFile.ser
Jython으로 실행하기> jython testHexView_03.py myFile.ser
The size of the file "myFile.ser" is 130.

0000 0000:  AC ED 00 05 73 72 00 06-50 65 72 73 6F 6E 07 31  |....sr..Person.1|
0000 0010:  46 DB A5 1D 44 AB 02 00-03 49 00 03 61 67 65 4C  |F...D....I..ageL|
0000 0020:  00 09 66 69 72 73 74 4E-61 6D 65 74 00 12 4C 6A  |..firstNamet..Lj|
0000 0030:  61 76 61 2F 6C 61 6E 67-2F 53 74 72 69 6E 67 3B  |ava/lang/String;|
0000 0040:  4C 00 08 6C 61 73 74 4E-61 6D 65 71 00 7E 00 01  |L..lastNameq.~..|
0000 0050:  78 70 00 00 00 13 74 00-05 4A 61 6D 65 73 74 00  |xp....t..Jamest.|
0000 0060:  04 52 79 61 6E 73 71 00-7E 00 00 00 00 00 1E 74  |.Ryansq.~......t|
0000 0070:  00 07 4F 62 69 2D 77 61-6E 74 00 06 4B 65 6E 6F  |..Obi-want..Keno|
0000 0080:  62 69                                            |bi              |

Read 130 bytes

 

 

 

Posted by Scripter
,