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

READBIN is a Text Console-based program which reads a single Input File specified on the command line one character at a time and prints out a formatted hex "dump" representation of the files contents 16 characters per display line. A prompt for continuation is issued after displaying 20 lines (320 characters of information). An entry of "X" or "x" followed by the <Enter> key terminates the program execution. Any other entry followed by <Enter> continues the display of the formatted hex and character information. A "." character is used for any non-displayable hex character.

Python, 230 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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# PROGRAM-NAME: readbin.py             BACKUP-REQUIRED: Yes
# AUTHOR: Tony Dycks                   REVISED BY: Tony Dycks
# DATE-WRITTEN: June 13, 2001          DATE-REVISED: June 20, 2001
# LANGUAGE: Active Python/Linux Python VERSION. NO.: 1.5.2 to 2.1
# PLATFORM: Win32/Linux (Text Console) VERSION. NO.: Any
#
# DESCRIPTION:
#   Display Hex And Character Contents Of File Entered On The Command Line.
#   Display 16 Characters Of Hex And Character Data Each Screen Line.  Pause
#   Display And Prompt For Continuation Every 20 Lines.  A Keyboard Entry
#   Of "X" <Enter> or "x" <Enter> Terminates The Program.  Any Other Key
#   Entry Continues The Display For Another 20 Lines.
#
# LIBRARY IMPORTS:
#   sys  Library Module For Screen I/O Functions And Exit To System
#   os   Library Module For Platform Clear Console Screen Function
#
# USAGE:
#   PYTHON readbin.py </Filepath/Filename.Ext> <Enter>   {Linux or Unix Implementations}
#   PYTHON readbin.py <D:\Filepath\Filename.Ext> <Enter> {Windows/Ms-DOS Implementations}
#
# USAGE EXAMPLE:
#   PYTHON readbin readbin.py <Enter>
#   {Displays This Source Program In ASCII and Hex Dump Format}
#
# REFERENCES: Adaptation of REXX Command Line Utility Program For Reading Binary
#   Files Using Python on a Win32 or Linux Terminal Console.
#
# TESTING SPECIFICS:
#   Program Has Been Tested On The Following Platforms And Versions Of Python: 
#
#   ----------------------------   ----------------------------------------------
#   Platform/Version               Python Version
#   ----------------------------   ----------------------------------------------
#   Red Hat Linux Version 6.1      Stichting Mathmatisch Centrum Amsterdam V1.5.2
#   Red Hat Linux Version 7.1      Stichting Mathmatisch Centrum Amsterdam V1.5.2
#   SuSE Linux Version 7.0         Stichting Mathmatisch Centrum Amsterdam V1.5.2
#   Microsoft Windows 98 SE        Active State Active Python Version 2.1
#   Microsoft Windows NT 4.0       Active State Active Python Version 2.1
#   Microsoft MS-DOS Version 7.0   Rational Systems, Inc. Version 1.97
#
# LIMITATIONS AND ASSUMPTIONS:
#   Execution From A Terminal Console OS Prompt Assumed.  Execution from within the
#   Python Interpreter Shell Requires Modification of argv Command Line
#   String Edits.  Program Has Been Tested On Terminal Console Windows For
#   The Following Platforms:  1) Red Hat Linux(tm) 6.1 Using Python Version 1.5.2,
#   2) SuSE Linux(tm) 7.0 Using Python Version 1.5.2, 3) Microsoft Windows 98 SE(tm)
#   Using Active State Active Python 2.1(tm).
#
#   Modification to the 'sys.platform' testing logic may be required for
#   implementations of Python, Windows, DOS or Linux that return a different
#   string value for the 'sys.platform' string property.
#
# PROGRAM MODIFICATION AND PLATFORM USAGE NOTES:
#   Use "import sys" and "print sys.platform" commands within Python Interpreter
#   To Determine Your  "sys.platform" value of Your Implementation Of Python If
#   Your OS Platform Is Something Other Than Red Hat Linux V6.1, SusE Linux V7.0,
#   or MS Windows 98 Second Edition under MS-DOS.  Platforms Other Than Linux,
#   Unix, Windows Or MS-DOS May Have A Value Other Than 'clear' or 'cls' For The
#   System Clear Terminal Screen Command.
#
# PROGRAM USE AND LICENSING:
#   Program is submitted under terms of the Python license and has been submitted
#   for consideration for inclusion in the upcoming O'Reilly Publication, 
#   "Python Cookbook".  Permission is granted for any editing or modification of 
#   the program for publication purposes.
#
# ==================================
# ==> Python Module Importations <==
# ==================================
import sys  # For OS Platform Determination
import os   # For Implementation of System Clear Terminal Screen Command,
            # Command Line Argument Processing and Exit To OS Prompt Function
# ================================
# ==> Constant Initializations <==
# ================================
dashln = '-' * 78
hexfmtlen = 6
# ==========================
# ==> Main Program Logic <==
# ==========================
#
# >>> Platform Testing <<<
#
# >>> Test Whether Program Is Being Run Under A Flavor Of Linux or Unix? <<<
# >>> Known Linux Implementations Tested Were Red Hat V6.1 and SuSE V7.0 <<<
if sys.platform == 'linux-i386' or sys.platform == 'linux2':
	SysCls = 'clear'
# >>> Test Whether Program Is Being Run Under Windows 32-bit Implementation <<<
elif sys.platform == 'win32' or sys.platform == 'dos' or sys.platform[0:5] == 'ms-dos':
	SysCls = 'cls'
# >>> Otherwise the Clear Screen Func is 'unknown' to prevent erroneous command execution <<<
else:
	SysCls = 'unknown'

def headings(argFile, argDashes, argSysCls): # Arguments are Filename, Line, Clr Scrn Fn Command
	if argSysCls != 'unknown':  # Is A Valid Clear Screen Command Known?
		os.system(argSysCls)      # Invoke System Clear Screen Display OS Command
	print 'File: ', argFile
	print argDashes
	return 0
	
def GetDisplayChar(argCharVal, argInChar): # Arguments are ASCII Char Value & Character Read
 	if charval < 32 or (charval > 127 and charval < 161):
		return '.'    # Use '.' to denote Non-displayable character
	else:
		return argInChar

if SysCls != 'unknown':  # Is A Valid Clear Screen Command Known?
	os.system(SysCls)      # Invoke System Clear Screen Display OS Command
# =================================
# ==> Print Program Info Banner <==
# =================================
print 'READBIN.PY -- Python Hex File Format Dump Command Line Utility For Binary Files'
print 'Version 1.0'
print 'By Tony Dycks'
print ' '
print 'Date Written: June 13, 2001'
print 'Date Last Revised: June 20, 2001'
print
print 'Submitted For Inclusion In The Python Cookbook'
print 'Open Source Contribution Under The GNU Public License'
print ' '
print ' '
print 'Running on System Platform:', sys.platform
print ' '
print 'Press <Enter> Key To Continue ... ',
response = sys.stdin.readline()
charln = ''
hexln = ''
offset = 0
offsetlen = 0
hexoffset = ''
charval = 0
charcnt = 0
# ################################################################################
# The following code will require mods if run from within the Python Interpreter
# ##############################################################################
# =========================================================================
# ==> No Command Line Argument Specified; Display Program Usage Message <==
# =========================================================================
if len(sys.argv) < 2:  
	print chr(7)  # ==> Beep Once <==
	print 'Incorrect Command Line Syntax ...'
	print ' '
	print 'Usage: Python readbin.py </Filepath/Filename.Ext> <Enter>'
	print ' '
	print sys.exit()
# =========================================================
# ==> Open File Specified On The Command Line For Input <==
# =========================================================
try:
	infile = open(sys.argv[1], 'r')   
	lncnt = headings(sys.argv[1], dashln, SysCls)  # Successful Return From Function Inits Line Counter
	while 1:  # ==> Read Until End Of File One Character At A Time <==
		inchar = infile.read(1)
		if not inchar:
			break
		charcnt = charcnt + 1
		charval = ord(inchar)
		hexval = hex(charval)
		hexstrlen = len(hexval)
		startpos = hexstrlen - 2
		if hexval[startpos] == 'x':
			startpos = startpos + 1
			hexval = '0'+hexval[startpos]
		else:
			hexval = hexval[startpos:hexstrlen]
		hexln = hexln+' '+hexval
		charln = charln + GetDisplayChar(charval, inchar)
		# ===================================================================
		# ==> 16 Characters Appended To String; Time To Print A Dump Line <==
		# ===================================================================
		if charcnt == 16:
			hexoffset = hex(offset)
			offsetlen = len(hexoffset)
			hexoffset = hexoffset[2:offsetlen]
			while len(hexoffset) < hexfmtlen:
				hexoffset = '0'+hexoffset
			print hexoffset+': '+hexln, ' | ', charln
			charln = ''
			hexln = ''
			charcnt = 0
			offset = offset + 16
			lncnt = lncnt + 1
			# ========================================================================
			# ==> A Screen-full of Info Displayed; Prompt for Continuation or Exit <==
			# ========================================================================
			if lncnt > 19:   
				print dashln
				print 'Press "X" <Enter> To Exit; <Enter> To Continue ... ',
				response = sys.stdin.readline()
				# =============================================================
				# ==> Take The First Character Slice of String, "response"; <==
				# ==> Test for Uppercase or Lowercase E<x>it response       <==
				# ======================================================--=====
				if response[0] == "X" or response[0] == 'x':
					break  # >>> Back to OS Console Prompt <<<
				else:
					# ====================================================
					# ==> Display Headings; Reset Screen Lines Counter <==
					# ====================================================
					lncnt = headings(sys.argv[1], dashln, SysCls)
except:
	print chr(7)  # >>> Beep Once <<<
	print 'Error Processing File:', sys.argv[1]
	print 'Terminating Program -- readbin.py'
	print ' '
	sys.exit()
# ============================
# ==> End Of Program Logic <==
# ============================
if len(charln) > 0:
	while len(charln) < 16:
		hexln = hexln + '   '
		charln = charln + ' '
		hexoffset = hex(offset)
		offsetlen = len(hexoffset)
		hexoffset = hexoffset[2:offsetlen]
	while len(hexoffset) < hexfmtlen:
		hexoffset = '0'+hexoffset
	print hexoffset+': '+hexln, ' | ', charln
# ===============================================
# ==> Successfully Processed The File Then    <==
# ==> Show Message and Exit Back To OS Prompt <==
# =============================================== 
print ' '
print '>>> End Of Program -- readbin.py <<<'
print ' '
sys.exit()

Readbin.py is a Python adaptation of a REXX program utility, which appears in the Perfect Niche Publication, "Down To Earth REXX" authored by William F. (Bill) Schindler, ISBN # 0-9677590-0-5 (c)2000. Program Listing 15.3 "Using CharIn to read a binary file" Pages 266-267.

Additional enhancements to the application include revisions to display the input filename at the top of the formatted dump display, use of the '|' character to segment the hex display portion from the character display portion of a dump line and expansion of the hex offset label to six hex characters to suit the display of larger program executable and object files.

I personally find the utility useful for identifying stub or embedded date/timestamp information within executable files or as a tool for identifying the ascii representation of character information in a text file. For Python programming it was useful for identifying mixes of tabs and spaces in decision block code which was converted differently by different text editors while porting Python source between my Windows 98 and Linux machines.

The program also illustrates the use of the "sys" and "os" Python modules for identifying the platform the utility is running on and using that information to determine what Clear Screen OS command to issue.

A great alternative implementation of this utility (and probably the next step I'll personally undertake in modifying the program as I learn Python) is to use Tkinter and place the hex and character display contents in a a textbox-type control with a scroll bar control on a GUI form with Exit and Page Forward/Backwards Buttons. The colorization of displayable versus non-displayable characters and the highlighting both the hex and character information of an given area selected with the mouse would facilitate reading and identifying the hex representation of given character information.

1 comment

Robin Parmar 22 years, 5 months ago  # | flag

alternate implementation. I was going to make some suggestions, such as adding a jump-to, command-line options, simplifying the code in various places, and making it all more Pythonic.

But instead I have modified my own hex dump utility to add your screen clearing. It can be found here as hexify.