Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jlaasonen committed Sep 20, 2021
0 parents commit 45b2dbc
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.exe
hexfile
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Jussi Laasonen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# hexfile

Simple command-line tool which prints a hex dump of a file.

## Requirements

[FreeBASIC-1.08.1](https://freebasic.net/)

## Compiling

```
fbc hexfile.bas
```

## Usage

```
hexfile <filename>
```

Pressing any key will print the next page.

![image](https://user-images.githubusercontent.com/404469/134071015-8a2486db-1a37-4dda-a92e-6ab37a1d312a.png)

## Acknowledgements

Inspired by the **hexfile** program in *P.K. McBride, 1989, Programming in GW-BASIC* chapter 15.6.
41 changes: 41 additions & 0 deletions hexfile.bas
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Const bytesPerLine = 16
Const nonAsciiByte = "."
Const asciiLowerBound = 31
Const asciiUpperBound = 127
Const hexColumn = 11
Const asciiColumn = 64

Dim consoleDimensions As integer = Width()
Dim consoleHeight As integer = HiWord(consoleDimensions)

Dim fileName As string = Command(1)
Dim fileNumber As Long = FreeFile
Open fileName For Binary Access Read As #fileNumber

Dim fileIndex As Integer = 1

Do
Dim asciiBytes As string = ""
Print Hex(fileIndex,6);Tab(hexColumn);

For byteIndex As integer = 1 To bytesPerLine
Dim byte_ As Ubyte
Get #fileNumber,,byte_

If byte_ > asciiLowerBound and byte_ < asciiUpperBound Then
asciiBytes += Chr(byte_)
Else
asciiBytes += nonAsciiByte
End If

Print Hex(byte_, 2);" ";
If byteIndex = bytesPerLine\2 Then Print " ";

fileIndex += 1
Next

Print Tab(asciiColumn);asciiBytes
If (fileIndex\bytesPerLine) mod (consoleHeight-1) = 0 Then Sleep
Loop Until Eof(fileNumber)

Close(fileNumber)

0 comments on commit 45b2dbc

Please sign in to comment.