Welcome, guest | Sign In | My Account | Store | Cart

This program will dump a file to the screen "classic" style.

Python, 48 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os, sys

def main():
    try:
        engine(file(' '.join(sys.argv[1:]), 'rb', 0).read())
    except:
        print os.path.basename(sys.argv[0]), '<filename>'

def engine(string):
    if len(string) > 0:
        parts = divide(string, 16)
        for index in range(len(parts)):
            print ' | '.join([hex(index << 4)[2:].upper().zfill(8)[-8:], \
                              pad_right(convert_hex(parts[index]), 47), \
                              convert_print(parts[index])])

def divide(string, length):
    parts = list()
    for index in range(len(string[:-1]) / length + 1):
        parts.append(string[index*length:index*length+length])
    return parts

def pad_right(string, length, padding=' '):
    if len(string) < length:
        return string + padding[0] * (length - len(string))
    else:
        return string

def convert_hex(string):
    return ' '.join([hex(ord(character))[2:].upper().zfill(2) \
                     for character in string])

def convert_print(string):
    characters = str()
    rule = can_print()
    for character in string:
        if character in rule:
            characters += character
        else:
            characters += '.'
    return characters

def can_print():
    return ''.join([chr(byte) for byte in range(256) \
                    if len(repr(chr(byte))) == 3 or byte == ord('\\')])
                    
if __name__ == '__main__':
    main()

This is just another HEX dumper. Have any suggestions?

2 comments

Stephen Chappell (author) 18 years, 4 months ago  # | flag

Second Version.

import os, sys

def main():
    try:
        engine(file(' '.join(sys.argv[1:]), 'rb', 0).read())
    except:
        print os.path.basename(sys.argv[0]), ''

def engine(string):
    if len(string) > 0:
        parts = divide(string, 16)
        rule = can_print()
        for index in range(len(parts)):
            print ' | '.join([hex(index)[2:].upper().zfill(8)[-8:] + '0', \
                              pad_right(convert_hex(parts[index]), 47), \
                              convert_print(parts[index], rule)])

def divide(string, length):
    return [string[index*length:index*length+length] \
            for index in range(len(string[:-1]) / length + 1)]

def pad_right(string, length, padding=' '):
        return string + padding[0] * (length - len(string))

def convert_hex(string):
    return ' '.join([hex(ord(character))[2:].upper().zfill(2) \
                     for character in string])

def convert_print(string, rule):
    return ''.join([character in rule and character \
                    or '.' for character in string])

def can_print():
    return ''.join([chr(byte) for byte in range(256) \
                    if len(repr(chr(byte))) == 3 or byte == ord('\\')])

if __name__ == '__main__':
    main()
Stephen Chappell (author) 18 years, 4 months ago  # | flag

Correction.

print ' | '.join([hex(index)[2:].upper().zfill(7)[-7:] + '0', \