PROGRAM hexdump;

{  This program will dump in hex and ascii form a file that the user    }
{  specifies.  Written for Turbo Pascal by Jeff Firestone.  June 1984.  }

CONST
  ArrSize = 128;
VAR
  argc, i, numin, tot, cfrom: INTEGER;
  f : FILE;
  Buffer : ARRAY [0..ArrSize] OF CHAR;


PROCEDURE ohib(nib:CHAR);
BEGIN
  i:= ORD(nib);
  i:= i AND 15;
  IF (i >= 10) THEN
    WRITE(CHR(i - 10 + ORD('A')))
  ELSE
    WRITE(i);
END;

PROCEDURE ohb(byt:CHAR);
VAR
  TmpByt : CHAR;
BEGIN
  TmpByt:= CHR(ORD(byt) SHR 4);
  ohib(TmpByt);
  ohib(byt);
END;

PROCEDURE ohw(wrd:INTEGER);
VAR
  TmpByt : CHAR;
BEGIN
  TmpByt:= CHR(HI(wrd));
  ohb(TmpByt);
  TmpByt:= CHR(LO(wrd));
  ohb(TmpByt);
END;


PROCEDURE OpenFile;
VAR Name : STRING[20];
BEGIN
  FILLCHAR(Buffer, SIZEOF(Buffer), 0);
  WRITE('What file would you like to dump in hex form : ');
  READLN(Name);
  ASSIGN(f, Name);
  RESET(f);
END;


PROCEDURE DoHexDump;
BEGIN
  tot:= 0;
  REPEAT
{  Read in a block of characters from the file  }

    BLOCKREAD(f, Buffer, 1);
    cfrom:= 0;
    WHILE cfrom < ArrSize-1 DO
    BEGIN

{  Print the offset in HEX  }

      ohw(tot);
      WRITE(' :');

{  Print the bytes in HEX  }

      FOR i:= 0 TO 7 DO
      BEGIN
        WRITE(' ');
        ohb(buffer[cfrom]);
        cfrom:= cfrom + 1;
      END;

      WRITE(' ');

      FOR i:= 8 TO 15 DO
      BEGIN
        WRITE(' ');
        ohb(buffer[cfrom]);
        cfrom:= cfrom + 1;
      END;

      cfrom:= cfrom - 16;
      WRITE('  '+CHR(186)+' ');

{  Print the bytes in ASCII  }

      FOR i:= 0 TO 15 DO
      BEGIN
        IF ((Buffer[cfrom] < ' ') OR (ORD(Buffer[cfrom]) >= $7F)) THEN
          Buffer[cfrom]:= '.';
        WRITE(Buffer[cfrom]);
        cfrom:= cfrom + 1;
      END;
      WRITELN(' '+CHR(186));
      tot:= tot + 16;
      IF (tot MOD 128) = 0 THEN WRITELN;
    END;   {  While  }
  UNTIL EOF(f);
  CLOSE(f);
END;


BEGIN
  OpenFile;
  DoHexDump;
END.
       RE