From 45b2dbc07bbbef9bc752542bf04d40ea8cd84c0b Mon Sep 17 00:00:00 2001 From: Jussi Laasonen <404469+jlaasonen@users.noreply.github.com> Date: Wed, 15 Sep 2021 14:38:14 +0200 Subject: [PATCH] Initial commit --- .gitignore | 2 ++ LICENSE | 21 +++++++++++++++++++++ README.md | 27 +++++++++++++++++++++++++++ hexfile.bas | 41 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 hexfile.bas diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..58f67e8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.exe +hexfile diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..842f973 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..95e1c43 --- /dev/null +++ b/README.md @@ -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 +``` + +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. diff --git a/hexfile.bas b/hexfile.bas new file mode 100644 index 0000000..972ee6a --- /dev/null +++ b/hexfile.bas @@ -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)