hexfile: a simple command line hex dumper

User projects written in or related to FreeBASIC.
Post Reply
jlaasonen
Posts: 4
Joined: Sep 20, 2022 19:10
Contact:

hexfile: a simple command line hex dumper

Post by jlaasonen »

Hi all,

this is my first FreeBASIC project. I first learned programming with GW-BASIC and after finding FreeBASIC, I wanted try programming with a modern BASIC. Inspired by the hexfile example in Programming in GW-BASIC (P.K. McBride, 1989) I decided to implement a simple hex dump tool. The program is now in state that is reasonably usable, so I decided to share it with the community.

Homepage: https://jlaasonen.me/software/hexfile
Source: https://github.com/jlaasonen/hexfile

Windows versions can be installed with Scoop:

Code: Select all

scoop bucket add jlaasonen https://github.com/jlaasonen/scoop-bucket
scoop install hexfile
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: hexfile: a simple command line hex dumper

Post by coderJeff »

Nice, I like it. Good job!

It seems weird though that the byte offset starts at one (1). Even though fbc file I/O statements expects first offset of a file as one (1) it seems more natural to display the offset starting at zero (0) -- probably because every hex view/dump program I've used starts at offset zero (0).

Code: Select all

 00000001   43 6F 6E 73 74 20 62 79 74  65 73 50 65 72 4C 69   Const bytesPerLi
 00000011   6E 65 20 3D 20 31 36 0D 0A  43 6F 6E 73 74 20 74   ne = 16..Const t
 00000021   61 62 57 69 64 74 68 20 3D  20 33 0D 0A 43 6F 6E   abWidth = 3..Con
 00000031   73 74 20 66 69 6C 65 49 6E  64 65 78 57 69 64 74   st fileIndexWidt
 
jlaasonen
Posts: 4
Joined: Sep 20, 2022 19:10
Contact:

Re: hexfile: a simple command line hex dumper

Post by jlaasonen »

It is a left over from the GW-BASIC example the first version was based on. I haven't changed it yet since Get expect 1 based offset, but I am planning to make it configurable.

Here are all the features I am currently thinking of implementing:
  • configurable first offset
  • configurable offset width
  • hide/show offset prefix zeroes
  • colors
  • option to dump the whole file without scrolling
  • command to jump to specific offset
jlaasonen
Posts: 4
Joined: Sep 20, 2022 19:10
Contact:

Re: hexfile: a simple command line hex dumper

Post by jlaasonen »

I made a new release (v1.3.0) yesterday: https://github.com/jlaasonen/hexfile/re ... tag/v1.3.0
The offset starts at zero by default and it is possible to give desired starting offset as an argument.
coderJeff
Site Admin
Posts: 4313
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: hexfile: a simple command line hex dumper

Post by coderJeff »

Thanks for the update. Looking good.

Eventually, you may want to make your command line processing somewhat flexible and with some user feedback. Just a suggestion.

Something like:

Code: Select all

dim opt_help as boolean
dim opt_start_index as integer
dim opt_filename as string
dim opt_have_filename as boolean

dim i as integer = 1
while i < __FB_ARGC__
	print command(i)
	select case left( command(i), 1 )
	case "-"
		select case command(i)
		case "-h", "-help", "--help"
			opt_help = true
		case "-i", "--index"
			i += 1
			opt_start_index = vallng( command(i) )
			if( opt_start_index < 0 ) then
				print "error: invalid start index " & command(i)
				end 1
			end if
		case else 
			print "error: invalid option '" & command(i) & "'"
			end 1
		end select
	case else
		if( opt_have_filename = false ) then
			if( command(i) > "" ) then
				opt_have_filename = true
				opt_filename = command(i)
			else
				print "error: empty file name"
				end 1
			end if
		else
			print "error: too many files at '" & command(i) & "'"  
			end 1
		end if
	end select
	i += 1
wend

print "opt_help          = " & opt_help
print "opt_start_index   = " & opt_start_index
print "opt_have_filename = " & opt_have_filename
print "opt_filename      = '" & opt_filename & "'"
jlaasonen
Posts: 4
Joined: Sep 20, 2022 19:10
Contact:

Re: hexfile: a simple command line hex dumper

Post by jlaasonen »

I made a bug-fix release (v1.3.1) today: https://github.com/jlaasonen/hexfile/re ... tag/v1.3.1

It fixes the following issues:
- Columns are not split evenly.
- 00 byte shown on lines after the end of the file.
- Index shown on lines after end of file.

Under the hood, I split the code into multiple files and added unit tests.

I plan to upgrade the command line processing when adding new options, but I haven't decided how to approach the problem yet. Might try to build a more generic configurable solution or go with the suggestion. I do not have much time for hobby projects so progress will be slow.

In addition, I am considering migrating the project away from big-tech GitHub to Codeberg or sourcehut. Both promise not to track or sell my data.
Post Reply