rhd/src/tobin.rs

26 lines
738 B
Rust

use std::io::{self, BufRead, stdout, Write};
fn dump_to_bin(line: String) {
let words: Vec<&str> = line.split(" ").collect();
if words.len() == 2 {
let iter = words[0].split_whitespace();
let data: Vec<&str> = iter.skip(1).collect();
let hex: String = data.join("");
let bytes = hex::decode(hex).unwrap();
let u8_values: Vec<u8> = bytes.iter().map(|b| *b).collect();
stdout().write_all(&u8_values).expect("Failed to write to stdout");
}
}
pub fn revert_hexdump(reader: Box<dyn io::BufRead>) {
for buf in reader.lines() {
match buf {
Ok(line) => { dump_to_bin(line) }
Err(_) => { eprintln!("Cannot read input file") }
}
}
}