refactor: add doc comment for split numbers and change order to msb first #76

This commit is contained in:
Christoph J. Scherr 2024-05-12 17:13:18 +02:00
parent 77d473e22c
commit 9da3c584cd
2 changed files with 19 additions and 3 deletions

View File

@ -3,9 +3,24 @@
//! Sometimes, you need a large integer in the form of many bytes, so split into [u8]. //! Sometimes, you need a large integer in the form of many bytes, so split into [u8].
//! Rust provides //! 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<T>(num: T) -> Vec<u8> pub fn unsigned_to_vec<T>(num: T) -> Vec<u8>
where where
u128: std::convert::From<T> u128: std::convert::From<T>
@ -19,5 +34,6 @@ where
buf.push(num as u8); buf.push(num as u8);
num >>= 8; num >>= 8;
} }
buf.reverse();
buf buf
} }

View File

@ -24,7 +24,7 @@ fn split_u64() {
let correct = [ let correct = [
vec![16], vec![16],
vec![255], vec![255],
vec![255, 1], vec![1, 255],
vec![0], vec![0],
vec![255, 255, 255, 255, 255, 255, 255, 255], vec![255, 255, 255, 255, 255, 255, 255, 255],
vec![255, 255, 255, 255], vec![255, 255, 255, 255],