From 97b80866249e11aaca0f84e0c7013b70080bc3eb Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 3 Mar 2024 17:16:50 +0100 Subject: [PATCH] fix bintolsdisplay --- members/libpt-bintols/src/display.rs | 11 ++++++----- members/libpt-bintols/tests/display.rs | 8 +++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/members/libpt-bintols/src/display.rs b/members/libpt-bintols/src/display.rs index 904190c..73e9630 100644 --- a/members/libpt-bintols/src/display.rs +++ b/members/libpt-bintols/src/display.rs @@ -8,11 +8,12 @@ pub use num_traits::{PrimInt, ToPrimitive}; /// ### Arguments /// * `data` - The data you are trying to dump pub fn bytes_to_bin(data: &[u8]) -> String { - let mut s = format!("0b{:08b}", data.first().unwrap()); - for dat in data { - s.push_str(&format!("_{:08b}", dat)); - if dat % 8 == 0 { - s.push('\n') + let mut s = String::new(); + for (i, dat) in data.iter().enumerate() { + if i == 0 { + s.push_str(&format!("0b{:08b}", dat)); + } else { + s.push_str(&format!("_{:08b}", dat)); } } s diff --git a/members/libpt-bintols/tests/display.rs b/members/libpt-bintols/tests/display.rs index c730a1c..89f3645 100644 --- a/members/libpt-bintols/tests/display.rs +++ b/members/libpt-bintols/tests/display.rs @@ -6,6 +6,10 @@ fn btobin() { let data = [19, 19]; let r = bytes_to_bin(&data); assert_eq!(r, format!("0b00010011_00010011")); + + let data = [0xff, 0xff]; + let r = bytes_to_bin(&data); + assert_eq!(r, format!("0b11111111_11111111")); } #[test] @@ -15,9 +19,7 @@ fn big_btobin() { assert_eq!( r, format!( - "0b00001100_00011111_01010010_\ - 00100000_01111011_00100000_01011100_00010111_00001100\n\ - _00100000_00001100_00000001_00000001_00000001" + "0b00001100_00011111_01010010_00100000_01111011_00100000_01011100_00010111_00001100_00100000_00001100_00000001_00000001_00000001" ) ); }