HEX VIEWER - intermediate level
by Walt Grams

Home

Liberty BASIC News
Safe Registry and Ini File Alternative
Deleting and Renaming Disk Files
Segments and Flushing
Flat Toolbar with Toolips
Translating 32-bit VB API Calls
Event-Driven Programming Concepts
Spotlight on the Community!
ODBC in Liberty BASIC
Hex Viewer
Listing Files Recursively
Registering Hot Keys
Preventing more than 1 instance
Multi-Coloured Text Input Boxes
Images on Buttons and Statictext
Two Demos by David Conner

HexView.bas by Walt Grams. Used by permission.
mailto:wjgrams@yahoo.com

Date: Sun, 06 Oct 2002
Subject: [libertybasic] HexView.bas

Here is a little program I wrote to allow one to view the contents of any file on a byte by byte basis. Each byte in the file is displayed as both a two digit hexadecimal number and the corresponding ASCII character. Try it on several different types of files to see what the various formats look like internally.

Just for fun try a Microsoft Word file. It is amazing what they keep in your files.

Anyone looking for a challenge can turn this into a full hexadecimal file editor. :-)

Walt

'Displays the contents of any file as the hexadecimal values
'of the individual data bytes in the file and also as the
'corresponding ASCII characters

dim s$(16)
dim h$(16)
NOMAINWIN
WindowWidth = 500
WindowHeight = 380
UpperLeftX = 200
UpperLeftY = 150
statictext #hexView.fname, "File Name: ", 5, 10, 350, 14
button #hexView.cf, "Choose File", [CFClicked], UL, 370, 7, 100, 25
button #hexView.nxt, "Next", [NextClicked], Ul, 370, 45, 0, 0
button #hexView.prv, "Previous", [PrevClicked], UL, 370, 75, 0, 0
statictext #hexView.alabel, "Hexadecimal address:", 370, 125, 150, 15
textbox #hexView.addr, 370, 145, 50, 25
button #hexView.go, "Go", [GoClicked], UL, 430, 145, 0, 0
textbox #hexView.field, 50, 45, 300, 270
statictext #hexView.hexlabel, "Hexadecimal View", 53, 32, 130, 14
statictext #hexView.textlabel, "Text View", 270, 32, 50, 14
statictext #hexView.addrlabel1, "000000:", 5, 50, 45, 14
statictext #hexView.addrlabel2, "000020:", 5, 114, 45, 14
statictext #hexView.addrlabel3, "000040:", 5, 178, 45, 14
statictext #hexView.addrlabel4, "000060:", 5, 242, 45, 14
open "Hex View" for window as #hexView
print #hexView, "trapclose [quit]"
print #hexView.field, "!font Courier_New 10";
wait

[quit]
close #hexView
end

[GoClicked]
print #hexView.addr, "!contents? hexAddr$";
startLoc = hexdec(hexAddr$)
if startLoc < 0 then startLoc = 0
if currentFile$="" then wait
goto [ShowData]

[PrevClicked]
startLoc = startLoc - 128
if startLoc < 0 then startLoc = 0
if currentFile$="" then wait
goto [ShowData]

[NextClicked]
startLoc = startLoc + 128
if currentFile$="" then wait
goto [ShowData]


[CFClicked]
filedialog "Open file to view", "*.*", fileName$

if fileName$="" then wait
startLoc = 0
print #hexView.nxt, "!locate 370, 45, 100, 25"
print #hexView.go, "!locate 430, 145, 40, 25"
currentFile$ = fileName$

[ShowData]
open currentFile$ for input as #f
print #hexView.fname, "File Name: " + currentFile$
if startLoc > 5000 then print #hexView.field, "One moment please ..."
if startLoc > 0 then
  for x = 1 to startLoc
    if eof(#f)=0 then x$ = input$(#f,1)
  next x
  print #hexView.prv, "!locate 370, 75, 100, 25"
else
  print #hexView.prv, "!locate 370, 75, 0, 0"
end if
hexaddr1$ = pad$(dechex$(startLoc))
hexaddr2$ = pad$(dechex$(startLoc + 32))
hexaddr3$ = pad$(dechex$(startLoc + 64))
hexaddr4$ = pad$(dechex$(startLoc + 96))
print #hexView.addrlabel1, hexaddr1$; ":"
print #hexView.addrlabel2, hexaddr2$; ":"
print #hexView.addrlabel3, hexaddr3$; ":"
print #hexView.addrlabel4, hexaddr4$; ":"
disp$ = ""
for index1 = 0 to 15
  s$(index1) = ""
  h$(index1) = ""
  for index2 = 0 to 7
    if eof(#f)=0 then
      val = asc(input$(#f,1))
      if val = 10 or val = 13 or val = 0 or val = 9 then
        s$(index1) = s$(index1) + " "
      else
        s$(index1) = s$(index1) + chr$(val)
      end if
      hexval$ = dechex$(val)
      if len(hexval$)<2 then hexval$ = "0" + hexval$
      h$(index1) = h$(index1) + hexval$ + " "
    else
      s$(index1) = s$(index1) + " "
      h$(index1) = h$(index1) + ".. "
    end if
    if index2=3 then h$(index1) = h$(index1) + " "
  next index2
  disp$ = disp$ + h$(index1) + "  " + s$(index1) + chr$(13) + chr$(10)
next index1
print #hexView.field, disp$
close #f
print #hexView, "refresh"
wait

function pad$(h$)
while len(h$) < 6
  h$ = "0" + h$
wend
pad$ = h$
end function

'end of program (watch for line wraps)
  



Home

Liberty BASIC News
Safe Registry and Ini File Alternative
Deleting and Renaming Disk Files
Segments and Flushing
Flat Toolbar with Toolips
Translating 32-bit VB API Calls
Event-Driven Programming Concepts
Spotlight on the Community!
ODBC in Liberty BASIC
Hex Viewer
Listing Files Recursively
Registering Hot Keys
Preventing more than 1 instance
Multi-Coloured Text Input Boxes
Images on Buttons and Statictext
Two Demos by David Conner