hexhex: a simple hex viewer library for testing purposes.
This commit is contained in:
commit
07517fbd6b
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/target
|
||||||
|
Cargo.lock
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal 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
40
src/lib.rs
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user