I'm trying to read a binary file in Rust and output the hex values of it on a file. I got most of the code working but got stuck on the "write" part:
use std::io::prelude::*;
use std::fs::File;
fn main() {
let f = File::open("input");
match f {
Ok(f) => {
let mut s = String::new();
for byte in f.bytes() {
s = s + &format!("{:02X} ", byte.unwrap());
}
let mut buffer = File::create("out.txt").unwrap();
buffer.write(s);
},
Err(_) => println!("Bad") // ignoring bindings
}
}
I am trying to write s
of type std::string::String
while buffer.write()
will only accept a primitive string of type &[u8]
. I've been scouring the docs for a while now to no avail so I decided to ask here.