binary_utils/
lib.rs

1use std::num::ParseIntError;
2
3fn split_string_with_spaces_for_substrings_with_length(s: &str, length: u64) -> String {
4    let string_with_spaces_seperating_substrings =
5        s.chars().enumerate().fold(String::new(), |acc, (i, c)| {
6            //if i != 0 && i == 11 {
7            if i != 0 && (i % length as usize == 0) {
8                format!("{} {}", acc, c)
9            } else {
10                format!("{}{}", acc, c)
11            }
12        });
13    string_with_spaces_seperating_substrings
14}
15
16pub fn decode_binary(s: &str) -> Result<Vec<u8>, ParseIntError> {
17    (0..s.len())
18        .step_by(9)
19        .map(|i| u8::from_str_radix(&s[i..i + 8], 2))
20        .collect()
21}
22
23pub fn split_binary_string_into_framents_of_11_bits(binary_string: &str) -> Vec<String> {
24    let entropy_plus_checksum_binary_with_spaces_seperating =
25        split_string_with_spaces_for_substrings_with_length(&binary_string, 11);
26    let word_binary: Vec<&str> = entropy_plus_checksum_binary_with_spaces_seperating
27        .split(" ")
28        .collect();
29    word_binary.iter().map(|&s| s.to_string()).collect()
30}
31pub fn split_binary_string_into_framents_of_5_bits(binary_string: &str) -> Vec<String> {
32    let entropy_plus_checksum_binary_with_spaces_seperating =
33        split_string_with_spaces_for_substrings_with_length(&binary_string, 5);
34    let word_binary: Vec<&str> = entropy_plus_checksum_binary_with_spaces_seperating
35        .split(" ")
36        .collect();
37    word_binary.iter().map(|&s| s.to_string()).collect()
38}
39
40pub fn convert_binary_to_int(binary_string: &str) -> isize {
41    let bin_idx = binary_string;
42    let intval = isize::from_str_radix(bin_idx, 2).unwrap();
43    intval
44}
45pub fn convert_to_binary_string(num: u8, bits_to_show_count: u64) -> String {
46    fn crop_letters(s: &str, pos: usize) -> &str {
47        match s.char_indices().skip(pos).next() {
48            Some((pos, _)) => &s[pos..],
49            None => "",
50        }
51    }
52    fn format_binary_with_4_bits(num: u8) -> String {
53        // The 06 pads with zeros to a width of 6. That width includes 0b (length=2)
54        format!("{:#06b}", num)
55    }
56    fn format_binary_with_8_bits(num: u8) -> String {
57        // The 10 pads with zeros to a width of 10. That width includes 0b (length=2)
58        format!("{:#010b}", num)
59    }
60    let binary_string_with_prefix = match bits_to_show_count {
61        4 => format_binary_with_4_bits(num),
62        8 => format_binary_with_8_bits(num),
63        _ => panic!(
64            "binary_string_without_prefix: bits_to_show_count of {} not supported",
65            bits_to_show_count
66        ),
67    };
68    let binary_string_without_prefix = crop_letters(&binary_string_with_prefix, 2);
69    binary_string_without_prefix.to_string()
70}
71
72pub fn get_binary_string_for_byte_array(byte_array: &Vec<u8>) -> String {
73    let mut binary_string = String::new();
74    for i in byte_array {
75        let binary_str = convert_to_binary_string(*i, 8);
76        binary_string.push_str(binary_str.as_str())
77    }
78    binary_string
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn it_works() {
87        // let result = add(2, 2);
88        // assert_eq!(result, 4);
89    }
90}