From b4228dec162698a824b9c6aff66f4014c6080aeb Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Sun, 12 May 2024 17:13:18 +0200 Subject: [PATCH] refactor: add doc comment for split numbers and change order to msb first --- members/libpt-bintols/src/split_numbers.rs | 20 ++++++++++++++++++-- members/libpt-bintols/tests/split_numbers.rs | 2 +- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/members/libpt-bintols/src/split_numbers.rs b/members/libpt-bintols/src/split_numbers.rs index 20cefa6..1405cbf 100644 --- a/members/libpt-bintols/src/split_numbers.rs +++ b/members/libpt-bintols/src/split_numbers.rs @@ -3,9 +3,24 @@ //! Sometimes, you need a large integer in the form of many bytes, so split into [u8]. //! Rust provides -/// split an integer into it's bytes, ignoring those bytes that would be all zero. + +/// Split unsigned integers into a [Vec] of [u8]s /// -/// If the integer is zero, the Vec contains a single null byte. +/// Say you have the [u32] 1717 (binary: `00000000 00000000 00000110 10110101 `). This number would +/// be splitted to `vec![0b00000110, 0b10110101]`. +/// +/// The 0 bytes of the numbers will be discarded (unless the number is 0, then the Vec contains a +/// single Null byte.) and the remaining parts of the numbers are inserted into a Vec as [u8]. +/// +/// # Examples +/// +/// ``` +/// # use libpt_bintols::split_numbers::*; +/// +/// let x: u32 = 1717; +/// +/// assert_eq!(unsigned_to_vec(x), vec![0b00000110, 0b10110101]); +/// ``` pub fn unsigned_to_vec(num: T) -> Vec where u128: std::convert::From @@ -19,5 +34,6 @@ where buf.push(num as u8); num >>= 8; } + buf.reverse(); buf } diff --git a/members/libpt-bintols/tests/split_numbers.rs b/members/libpt-bintols/tests/split_numbers.rs index 52b5ac5..2ee6a13 100644 --- a/members/libpt-bintols/tests/split_numbers.rs +++ b/members/libpt-bintols/tests/split_numbers.rs @@ -24,7 +24,7 @@ fn split_u64() { let correct = [ vec![16], vec![255], - vec![255, 1], + vec![1, 255], vec![0], vec![255, 255, 255, 255, 255, 255, 255, 255], vec![255, 255, 255, 255],