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

This recipe implements a simple data dump tool, roughly like the od command of Unix, which stands for octal dump (though od can also dump data in hex and other formats). This tool dumps data in character and hex formats, in this version. This is data_dump.py version 1.

Python, 88 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'''
Program name: data_dump.py
Version: 1
Author: Vasudev Ram.
Copyright 2015 Vasudev Ram.
Purpose: To dump the contents of a specified file or standard input, 
to the standard output, in one or more formats, such as:
    - as characters
    - as decimal numbers
    - as hexadecimal numbers
    - as octal numbers
    
Inspired by the od (octal dump) command of Unix, and intended to work,
very roughly, like it. Will not attempt to replicate od exactly or even 
closely. May diverge from od's way of doing things, as desired.
'''

# Imports:

from __future__ import print_function
import sys

# Global constants:

# Maximum number of character (from the input) to output per line.
MAX_CHARS_PER_LINE = 16

# Global variables:

# Functions:

def data_dump(infil, line_len=MAX_CHARS_PER_LINE, options=None):
    '''
    Dumps the data from the input source infil to the standard output.
    '''
    byte_addr = 0
    buf = infil.read(line_len)
    # While not EOF.
    while buf != '':
        # Print the offset of the first character to be output on this line.
        # The offset refers to the offset of that character in the input,
        # not in the output. The offset is 0-based.
        sys.stdout.write("{:>08s}: ".format(str(byte_addr)))

        # Print buf in character form, with . for control characters.
        # TODO: Change to use \n for line feed, \t for tab, etc., for 
        # those control characters which have unambiguous C escape 
        # sequences.
        byte_addr += len(buf)
        for c in buf:
            sys.stdout.write('  ') # Left padding before c as char.
            if (0 <= ord(c) <= 31) or (c == 127):
                sys.stdout.write('.')
            else:
                sys.stdout.write(c)
        sys.stdout.write('\n')

        # Now print buf in hex form.
        sys.stdout.write(' ' * 10) # Padding to match that of byte_addr above.
        for c in buf:
            sys.stdout.write(' ') # Left padding before c in hex.
            sys.stdout.write('{:>02s}'.format((hex(ord(c))[2:].upper())))
        sys.stdout.write('\n')
        buf = infil.read(line_len)
    infil.close()


def main():
    '''
    Checks the arguments, sets option flags, sets input source.
    Then calls data_dump() function with the input source and options.
    '''
    try:
        lsa = len(sys.argv)
        if lsa == 1:
            # Input from standard input.
            infil = sys.stdin
        elif lsa == 2:
            # Input from a file.
            infil = open(sys.argv[1], "rb")
        data_dump(infil)
        sys.exit(0)
    except IOError as ioe:
        print("Error: IOError: " + str(ioe))
        sys.exit(1)

if __name__ == '__main__':
    main()

The program can be run like this:

$ python data_dump.py some_file.txt

It will give output like this sample:

$ data_dump.py t3 00000000: T h e q u i c k b r o w n 54 68 65 20 71 75 69 63 6B 20 62 72 6F 77 6E 20 00000016: f o x j u m p e d o v e r 66 6F 78 20 6A 75 6D 70 65 64 20 6F 76 65 72 20 00000032: t h e l a z y d o g . . . T 74 68 65 20 6C 61 7A 79 20 64 6F 67 2E 0D 0A 54 00000048: h e q u i c k b r o w n f 68 65 20 71 75 69 63 6B 20 62 72 6F 77 6E 20 66 00000064: o x j u m p e d o v e r t 6F 78 20 6A 75 6D 70 65 64 20 6F 76 65 72 20 74 00000080: h e l a z y d o g . . . T h 68 65 20 6C 61 7A 79 20 64 6F 67 2E 0D 0A 54 68 00000096: e q u i c k b r o w n f o 65 20 71 75 69 63 6B 20 62 72 6F 77 6E 20 66 6F 00000112: x j u m p e d o v e r t h 78 20 6A 75 6D 70 65 64 20 6F 76 65 72 20 74 68 00000128: e l a z y d o g . 65 20 6C 61 7A 79 20 64 6F 67 2E

More details and discussion at this URL:

http://jugad2.blogspot.in/2015/11/datadump-python-tool-like-unix-od-octal.html

This is version 1 of data_dump.py.