The standard write_all and write functions from the std::fs crate allow to write string slices (so technically &[u8]?) to files.
Is there a way to write hex-numbers directly to the file?
The standard write_all and write functions from the std::fs crate allow to write string slices (so technically &[u8]?) to files.
Is there a way to write hex-numbers directly to the file?
You can use this function:
use std::io::{Result, BufWriter, Write};
fn write_hex<W: Write>(file: &mut BufWriter<W>, data: &[u8]) -> Result<()> {
for val in data {
write!(file, "{:02X}", val)?;
}
Ok(())
}
Since it writes each hex number individually, it requires that you use a buffered stream. You can create one with BufWriter::new(file)
. You can change {:02X}
to {:02x}
to write lower case hex instead.