1pub fn u8_size_in_kb(vec: &Vec<u8>) -> f64 {
3 let size_in_bytes = vec.len();
4 let size_in_kb = size_in_bytes as f64 / 1024.0;
5 size_in_kb
6}
7
8pub fn vec_u32_to_string(vec: &Vec<u32>) -> String {
10 let chars: Vec<char> = vec
11 .iter()
12 .map(|u| std::char::from_u32(u.clone()).unwrap_or(' '))
13 .collect();
14 chars.into_iter().collect()
15}
16
17pub fn read_binary_file(file_path: &str) -> std::io::Result<Vec<u8>> {
19 use std::io::Read;
20 let mut file = std::fs::File::open(file_path)?;
21 let mut buffer = Vec::new();
22 file.read_to_end(&mut buffer)?;
23 Ok(buffer)
24}
25
26pub fn u32_array_to_u8_array(input: &[u32]) -> Vec<u8> {
28 let mut output = Vec::with_capacity(input.len() * 4);
29 for &num in input {
30 output.push(num as u8);
31 output.push((num >> 8) as u8);
32 output.push((num >> 16) as u8);
33 output.push((num >> 24) as u8);
34 }
35 output
36}
37
38pub fn u8_array_to_u32_array(arr: &[u8]) -> Vec<u32> {
40 assert!(arr.len() % 4 == 0, "File length is not a multiple of 4");
41 arr.chunks(4)
42 .map(|chunk| {
43 u32::from_be_bytes([chunk[3], chunk[2], chunk[1], chunk[0]])
44 })
45 .collect()
46}
47pub fn u8_array_to_u16_array(arr: &[u8]) -> Vec<u16> {
49 assert!(arr.len() % 2 == 0, "Array length is not a multiple of 2");
50 arr.chunks(2)
51 .map(|chunk| u16::from_be_bytes([chunk[1], chunk[0]]))
52 .collect()
53}
54pub fn output_file(file_path: &str, buffer: &Vec<u8>) -> std::io::Result<()> {
56 use std::fs::File;
57 use std::io::Write;
58 use std::path::Path;
59 let path = Path::new(file_path);
60
61 if let Some(parent) = path.parent() {
62 std::fs::create_dir_all(parent)?;
63 }
64 let mut file = File::create(path)?;
65 file.write_all(buffer)?;
66 Ok(())
67}
68
69pub fn slice_string(s: &str, start: isize, end: isize) -> String {
71 let len = s.chars().count() as isize;
72 let start = if start < 0 { len + start } else { start } as usize;
73 let end = if end < 0 { len + end } else { end } as usize;
74
75 let start = start.min(len as usize).max(0);
77 let end = end.min(len as usize).max(start);
78
79 s.chars().skip(start).take(end - start).collect()
80}
81
82#[test]
83fn test_slice() {
84 let text = "Hello, world!";
85 assert_eq!("ello, worl", slice_string(text, 1, -2)); }
87
88#[cfg(test)]
89mod tests {
90 use super::*;
91
92 #[test]
93 fn test_u8_size_in_kb() {
94 let vec = vec![0; 2048]; assert_eq!(u8_size_in_kb(&vec), 2.0);
96 }
97
98 #[test]
99 fn test_vec_u32_to_string() {
100 let vec = vec![72, 101, 108, 108, 111]; assert_eq!(vec_u32_to_string(&vec), "Hello");
102
103 let vec_with_invalid = vec![72, 101, 108, 108, 111, 0x110000]; assert_eq!(vec_u32_to_string(&vec_with_invalid), "Hello ");
105 }
106
107 #[test]
108 fn test_read_binary_file() {
109 use std::io::Write;
110 let file_path = "test.bin";
111 let data = vec![1, 2, 3, 4, 5];
112
113 {
115 let mut file = std::fs::File::create(file_path).unwrap();
116 file.write_all(&data).unwrap();
117 }
118
119 let result = read_binary_file(file_path).unwrap();
121 assert_eq!(result, data);
122
123 std::fs::remove_file(file_path).unwrap();
125 }
126
127 #[test]
128 fn test_u32_array_to_u8_array() {
129 let input = vec![0x12345678, 0x90abcdef];
130 let expected_output =
131 vec![0x78, 0x56, 0x34, 0x12, 0xef, 0xcd, 0xab, 0x90];
132 let output = u32_array_to_u8_array(&input);
133 assert_eq!(output, expected_output);
134 }
135
136 #[test]
137 fn test_u8_array_to_u32_array() {
138 let input = vec![0x78, 0x56, 0x34, 0x12, 0xef, 0xcd, 0xab, 0x90];
139 let expected_output = vec![0x12345678, 0x90abcdef];
140 let output = u8_array_to_u32_array(&input);
141 assert_eq!(output, expected_output);
142 }
143
144 #[test]
145 fn test_round_trip_conversion() {
146 let input = vec![0x12345678, 0x90abcdef, 0xdeadbeef];
147 let u8_array = u32_array_to_u8_array(&input);
148 let output = u8_array_to_u32_array(&u8_array);
149 assert_eq!(input, output);
150 }
151}