hexhex: a simple hex viewer library for testing purposes.

This commit is contained in:
ddidderr 2022-04-05 08:18:12 +02:00
commit 07517fbd6b
Signed by: ddidderr
GPG Key ID: 3841F1C27E6F0E14
3 changed files with 50 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
Cargo.lock

8
Cargo.toml Normal file
View File

@ -0,0 +1,8 @@
[package]
name = "hexhex"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

40
src/lib.rs Normal file
View File

@ -0,0 +1,40 @@
fn print_printable_or_dot(chunk: &[u8]) {
for (idx, byte) in chunk.iter().enumerate() {
if idx > 0 && idx % 8 == 0 {
print!(" ");
}
if *byte >= 32 && *byte <= 126 {
print!("{}", String::from_utf8(vec![*byte]).unwrap());
} else {
print!(".");
}
}
}
fn print_offset(offset: usize) {
print!("{:08x} ", offset);
}
fn print_data_hex(chunk: &[u8]) {
for (idx, byte) in chunk.iter().enumerate() {
if idx > 0 && idx % 8 == 0 {
print!(" ");
}
print!("{:02x} ", byte);
}
}
pub fn print_hex(data: &[u8]) {
let mut offset = 0usize;
let chunks: Vec<&[u8]> = data.chunks(16).collect();
for chunk in chunks {
print_offset(offset);
print_data_hex(chunk);
print_printable_or_dot(chunk);
println!();
offset += 16;
}
}