reed_solomon_cx/field_math/
number_to_binary_poly.rs1pub fn number_to_binary_poly(num: u32) -> String {
2 let binary_string = format!("{num:b}");
3
4 let b_chars = binary_string.chars();
5 let max_index = b_chars.clone().count() - 1;
6
7 let mut result_str = String::new();
8 let mut last_op = "";
9 for (index, ch) in b_chars.enumerate() {
10 if ch == '0' {
11 continue;
12 } else {
13 result_str.push_str(last_op);
14 last_op = "+";
15 let x_index = max_index - index;
16 if x_index == 0 {
17 result_str.push('1');
18 } else if x_index == 1 {
19 result_str.push('x');
20 } else {
21 result_str.push('x');
22 result_str.push_str(&(max_index - index).to_string());
23 }
24 }
25 }
26
27 result_str
28}
29
30#[cfg(test)]
31mod tests {
32 use super::*;
33
34 #[test]
35 fn test_number_to_binary_poly() {
36 assert_eq!("x3+x", number_to_binary_poly(10));
37 assert_eq!("x3+x2+1", number_to_binary_poly(13));
38 assert_eq!("x3+x2", number_to_binary_poly(12));
39 assert_eq!("1", number_to_binary_poly(1));
40 assert_eq!("x", number_to_binary_poly(2));
41 }
42}