feat/bintols/split-numbers-to-bytes #77

Merged
PlexSheep merged 10 commits from feat/bintols/split-numbers-to-bytes into devel 2024-05-12 17:41:12 +02:00
2 changed files with 29 additions and 0 deletions
Showing only changes of commit 0eb6490854 - Show all commits

View File

@ -25,3 +25,4 @@ pub const YOBI: u128 = 2u128.pow(80);
// use libpt_core; // use libpt_core;
pub mod datalayout; pub mod datalayout;
pub mod display; pub mod display;
pub mod split_numbers;

View File

@ -0,0 +1,28 @@
//! # Split numbers into bits and bytes
//!
//! Sometimes, you need a large integer in the form of many bytes, so split into [u8].
//! Rust provides
use num_traits::Unsigned;
/// split an integer into it's bytes, ignoring those bytes that would be all zero.
///
/// If the integer is zero, the Vec contains a single null byte.
pub fn unsigned_to_vec<T>(mut num: T) -> Vec<u8>
where
T: Unsigned,
T: std::ops::ShrAssign<i32>,
T: std::cmp::PartialOrd<i32>,
u8: std::convert::From<T>,
T: Copy,
{
if num == 0 {
return vec![0];
}
let mut buf: Vec<u8> = Vec::new();
while num > 0 {
buf.push(num.into());
num >>= 8;
}
buf
}