I made this program because - would you believe it - I don't have any hex editors installed on my machine currently. I wanted to visualize the data in a file format for an incredibly ancient video game that I'm trying to reverse engineer so that I can make a program on DBPro that will load up the game's maps.
Anyway, it's a small enough program and I thought it might be useful to someone, so I'm posting it here.
This is a simple binary file viewer that prints the file to the screen. It has two display modes. Mode 1 that shows the hex address of where that line starts, the hex value of each byte on the left with the text data on the right. Mode 2 displays the text data along with the position in the file that the line starts at.
The snippet has been updated to show the address in both modes and to represent all numeric data as hex. Also, it now lets you drag and drop files on the executable to allow you to load any file easily. Also changed the loop so that the addresses displayed are correct. Also, the left/right keys always work now.
It shouldn't be too difficult to convert it into an actual hex editor.
Controls are:
1: Switches to Mode 1
2: Switches to Mode 2
Page Up/Down scrolls the data by a whole page.
Home resets to the first position.
Up/Down Arrow keys move the data line by line
Left/Right arrow keys advance or retreat the position by one.
filename as string : filename = "file.bin"
`set up keycode constants
#constant KEY_1 = 2
#constant KEY_2 = 3
#constant KEY_HOME = 199
#constant KEY_PAGEUP = 201
#constant KEY_PAGEDOWN = 209
#constant KEY_UP = 200
#constant KEY_LEFT = 203
#constant KEY_DOWN = 208
#constant KEY_RIGHT = 205
sync on
filename as string : filename = CL$()
if left$(filename, 1) = chr$(34) then filename = right$(filename, len(filename) - 1)
if right$(filename, 1) = chr$(34) then filename = left$(filename, len(filename) - 1)
`exit prompt filename, "file name"
if filename = "" then end
open to read 1, filename
make memblock from file 1, 1
outp as string
linenum as dword
offset as dword
lastkey as dword
mode = 1
linelen = 16
do
if scancode() = 0
if lastkey = KEY_UP
if offset > linelen then offset = offset - linelen
endif
if lastkey = KEY_DOWN
offset = offset + linelen
endif
if lastkey = KEY_PAGEUP
if offset > ((linelen * 24) - 1)
offset = offset - (linelen * 24)
else
offset = 0
endif
endif
if lastkey = KEY_PAGEDOWN
offset = offset + (linelen * 24)
endif
if lastkey = KEY_HOME
offset = 0
endif
if lastkey = KEY_RIGHT
offset = offset + 1
endif
if lastkey = KEY_LEFT
if offset > 0
offset = offset - 1
endif
endif
if lastkey = KEY_1
mode = 0
linelen = 16
endif
if lastkey = KEY_2
linelen = 64
mode = 1
endif
lastkey = 0
else
if keystate(KEY_UP) then lastkey = KEY_UP
if keystate(KEY_DOWN) then lastkey = KEY_DOWN
if keystate(KEY_PAGEUP) then lastkey = KEY_PAGEUP
if keystate(KEY_PAGEDOWN) then lastkey = KEY_PAGEDOWN
if keystate(KEY_HOME) then lastkey = KEY_HOME
if keystate(KEY_RIGHT) then lastkey = KEY_RIGHT
if keystate(KEY_LEFT) then lastkey = KEY_LEFT
if keystate(KEY_1) then lastkey = KEY_1
if keystate(KEY_2) then lastkey = KEY_2
endif
cls
if mode = 0
set cursor 0, 0
for i = 0 to 23
outp = ""
for j = 0 to 15
outp = outp + pad2(memblock byte(1, offset + (linelen * i) + j)) + " "
next j
print pad8(offset + (linelen * i)) + ": " + outp
next i
set cursor 508, 0
for i = 0 to 23
set cursor 508, i * 15
outp = ""
for j = 0 to 15
outp = outp + txt(memblock byte(1, offset + (linelen * i) + j))
next j
print outp
next i
endif
if mode = 1
set cursor 0, 0
for i = 0 to 23
outp = ""
for j = 0 to 63
outp = outp + txt(memblock byte(1, offset + (64 * linenum) + (linelen * i) + j))
next j
print pad8(offset + (linelen * i)) + ": " + outp
next i
endif
sync
loop
function pad2(inp as byte)
outp as string
outp = hex$(inp)
l = len(outp)
if 2 - l <> 0
`for i = 0 to (1 - l)
outp = "0" + outp
`next i
endif
endfunction outp
function pad8(inp as dword)
outp as string
outp = hex$(inp)
l = len(outp)
if 8 - l <> 0
for i = 0 to (7 - l)
outp = "0" + outp
next i
endif
endfunction outp
function txt(inp as byte)
outp as string
outp = str$(inp)
l = len(outp)
chr as string:chr = chr$(inp):if inp < 32 then chr = " "
outp = chr
endfunction outp
Everything I needed to know about trigonometry, I learned by becoming a game programmer.