Skip to content

Commit

Permalink
successfully write binary to file
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgraham4401 committed Nov 14, 2023
1 parent 4b5d186 commit ab478dd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 151 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
**/*.rdb
*.DS_Store
**/*.bak
**/.idea
**/.idea/*
**/.vscode
/target
/Cargo.lock
.idea/**
.idea/workspace.xml
**/*.bin
148 changes: 0 additions & 148 deletions .idea/workspace.xml

This file was deleted.

41 changes: 40 additions & 1 deletion src/gather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Gather<T> {
}

#[allow(dead_code)]
pub fn read_binary_file(file_name: &str) -> Result<Vec<u8>, String>{
fn read_binary_file(file_name: &str) -> Result<Vec<u8>, String>{
let raw_data = fs::read(file_name);
let data = match raw_data {
Ok(data) => data,
Expand All @@ -27,10 +27,49 @@ pub fn read_binary_file(file_name: &str) -> Result<Vec<u8>, String>{
Ok(data)
}

#[allow(dead_code)]
fn write_binary_to_file(file_path: &str, data: Vec<u8>) -> std::io::Result<()> {
fs::write(file_path, data)?;
Ok(())
}

#[allow(dead_code)]
fn group_u8_to_f64(bytes: &[u8]) -> Vec<f64> {
bytes
.chunks_exact(8) // Take chunks of 8 bytes
.map(|chunk| {
let mut value_bytes = [0; 8];
value_bytes.copy_from_slice(chunk);
f64::from_bits(u64::from_le_bytes(value_bytes))
})
.collect()
}

#[allow(dead_code)]
fn split_f64_to_u8(values: &[f64]) -> Vec<u8> {
values
.iter()
.flat_map(|&f| {
let bits = f.to_bits();
(0..8).map(move |shift| ((bits >> (shift * 8)) & 0xFF) as u8)
})
.collect()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn writes_binary_file() {
let double_data: Vec<f64> = vec![10.352012, -5.0272, 3.0, -2.0899];
let bytes = split_f64_to_u8(&double_data);
let file_path: &str = "./testTrace.bin";
let raw_data = write_binary_to_file(file_path, bytes);
println!("Gather: {:?}", raw_data)
}


#[test]
fn reads_binary_file() {
let file_path: &str = "./resources/shot6220.bin";
Expand Down

0 comments on commit ab478dd

Please sign in to comment.