This project is a command-line utility written in Rust that reads a file and outputs its contents in little-endian hexadecimal format (hexdump). The program allows an optional argument to limit the number of bytes read from the file. It is modeled after the Linux implementation of hexdump.
- Outputs the content of a file in a hexadecimal format, similar to the Linux
hexdumpcommand. - Supports reading the full file or a specified number of bytes using the
-nflag. - Handles common errors such as invalid arguments or file errors gracefully.
- Includes unit tests for argument parsing and hexdump output.
./hexdump [-n LEN] FILEFILE: Path to the file to be read.-n LEN: Optional flag to specify the number of bytes to read from the file.
-
Read the entire file:
./hexdump file.txt
This will print the entire contents of
file.txtin hexadecimal format. -
Read only the first 100 bytes:
./hexdump -n 100 file.txt
This will limit the output to the first 100 bytes of
file.txt.
- If incorrect usage is detected (e.g., missing file or
-nflag without a valid length), an error message is printed and the program exits with status code1. - If an invalid length is provided for the
-nflag, an error message is shown.
For a file with the following content in bytes: 00 01 02 03:
00000000 0100 0302For larger files, the output will be formatted in 16-byte chunks per line.
This utility depends on:
std::envfor argument handling.std::fs::Filefor file operations.std::io::{self, Read}for reading files and handling I/O.
- Install Rust if you haven't already: https://www.rust-lang.org/tools/install
- Clone the repository:
git clone https://github.com/Gamacore/rust-hexdump.git
- Navigate to the directory:
cd rust-hexdump - Build the project:
cargo build --release
- Run the executable:
./target/release/hexdump [-n LEN] FILE
This project includes a suite of unit tests to ensure the correctness of the hexdump function and argument parsing. To run the tests, use:
cargo testThis project is licensed under the MIT License.