Cute little HexDump tool

Here you can link to screenshots and demos etc. of programs made using BBC BASIC
User avatar
hellomike
Posts: 192
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Cute little HexDump tool

Post by hellomike »

Quite often I need to glance at the file content of a non-text file. Just to get an idea of the kind of information in it. E.g. what end-of-line bytes it uses.
So I need a hex-editor/viewer of some kind.

There are plenty of tools but pretty much all of them requires installation and has lots of functionality I don't require as I just need to glace at the content. That's all.
Some years ago I made this little BB4W program and now decided to publish it for the benefit of others.

Code: Select all

      MODE 12 : OFF
      W%=@vdu%!208 / @vdu%!216 : H%=@vdu%!212 / @vdu%!220 - 2 : S%=W%*H%/4
      DIM C&(W%/2-1),B% 260
      *SYS 1
      SYS "DragAcceptFiles",@hwnd%,1 : PRINT "Drop file onto this screen to display."

      ON SYS RETURN
      REPEAT
        WAIT 20
        Q%=INKEY(0)
        IF @msg%=563 THEN
          K%%=0 : Q%=0
          SYS "DragQueryFile",@wparam%,0,B%,260
          SYS "DragFinish",@wparam%
          @msg%=0
        ENDIF
        IF Q%=130 IF K%%       K%%=0              : Q%=0
        IF Q%=131 IF E%%-1>L%% K%%=INT(E%%/S%)*S% : Q%=0
        IF Q%=132 IF K%%       K%%-=S%            : Q%=0
        IF Q%=133 IF E%%-1>L%% K%%=L%%+1          : Q%=0
        IF Q%=0 THEN
          CLS : R%=0 : T%=0
          IF INSTR($$B%,".")=0 $$B%+="."
          F%=OPENIN$$B%
          PTR#F%=K%% : E%%=EXT#F%
          WHILE NOT EOF#F% AND VPOS<H%
            C%=BGET#F%
            C&(POS/2)=C%
            VDU 17,2,17,128
            IF C%<32  VDU 17,  1 : C%+=64  : R%+=1
            IF C%>127 VDU 17,135 : C%-=128 : T%+=1
            IF C%=127 VDU 17,134 : C%=32
            IF C%<32  VDU 17,  1 : C%+=64
            VDU C%,32
            IF POS=0 OR EOF#F% THEN
              IF POS=0 Z%=W%/2 ELSE Z%=POS/2:PRINT
              VDU 17,7,17,128
              FOR I%=0 TO Z%-1 : PRINT CHR$17 CHR$(7+8*(I% MOD 2)) RIGHT$("0"+STR$~C&(I%),2); : NEXT
            ENDIF
          ENDWHILE
          CLOSE#F%
          IF E%%>K%%+S% L%%=K%%+S%-1 ELSE L%%=E%%-1
          SYS "SetWindowText",@hwnd%,"Byte "+STR$(K%%+1)+" until "+STR$(L%%+1)+" from "+STR$E%%+". Displayed are "+STR$(L%%-K%%+1)+\
          \                          " bytes (including "+STR$R%+" CTRL and "+STR$T%+" high). ["+$$B%+"]"
          IF K%%>0     PRINT TAB(0,H%+1) CHR$17 CHR$3 "[Page Up]     [Home]";
          IF E%%-1>L%% PRINT TAB(W%-21,H%+1) CHR$17 CHR$3 "[End]     [Page Down]";
        ENDIF
      UNTIL FALSE
It has support for files without an extension and files >2GB.

I hope you find it useful and please let me know when you discover a problem/bug or have a suggestion for how to improve the coding itself.

Regards,

Mike
Edja
Posts: 64
Joined: Tue 03 Apr 2018, 12:07
Location: Belgium

Re: Cute little HexDump tool

Post by Edja »

Hi Mike,
I've just tried it out and it works nicely. And simple to use.
I have no immediate use for it right now but I've saved it. It may come in handy sooner or later.
Also your use of
SYS "DragQueryFile",@wparam%,0,B%,260 and SYS "DragFinish",@wparam%
is useful to me.
It seems I always find something interesting to learn in posted programs.
Thanks for posting this one.
regards,
Edja
RichardRussell

Re: Cute little HexDump tool

Post by RichardRussell »

hellomike wrote: Sat 30 Nov 2019, 10:52I hope you find it useful and please let me know when you discover a problem/bug or have a suggestion for how to improve the coding itself.
Not bugs as such, but a couple of observations:
  1. The window won't fit on my (1280 x 720, after automatic High DPI scaling) laptop screen. Since it scrolls an entire window at a time, there are bytes in the file I can never see!

  2. File pointers universally start at zero, not one, so when it says in the title bar "Byte 1 until 1380" I would argue it should say "Byte 0 until 1379".
User avatar
hellomike
Posts: 192
Joined: Sat 09 Jun 2018, 09:47
Location: Amsterdam

Re: Cute little HexDump tool

Post by hellomike »

Thanks to both of you for the replies.

HexDump does not dynamically select the best MODE, however, I did make it mode-independent, that is, you can select any 16 colour MODE. So for small (laptop) screens, MODE 3 would suit better.
Also byte count does usually starts with zero, but my setup was that I want to check 'first or second' byte in the file, hence the numbering.

The biggest theoretical flaw in the current program would be not storing @wparam% in ON SYS but in practice that never posed a problem.

Regards,

Mike