133 lines
3.6 KiB
Rust
133 lines
3.6 KiB
Rust
pub struct NumberSystemConversion {}
|
|
|
|
impl NumberSystemConversion {
|
|
/// byte_to_bool
|
|
///
|
|
/// Convert a u8 byte into a set of bool of the value
|
|
///
|
|
/// ex: 0xff -> true, true, true, true, true, true, true, true
|
|
/// 0xa0 -> true, false, true, false, false, false, false, false
|
|
pub fn byte_to_bool(from: u8) -> [bool; 8] {
|
|
let mut result = [false; 8];
|
|
for i in 0..8 {
|
|
// Extract the i-th bit and convert it to a boolean
|
|
result[i] = ((from >> i) & 1) != 0;
|
|
}
|
|
result
|
|
}
|
|
|
|
/// bool_to_byte
|
|
///
|
|
/// Convert a set of bool to a single byte
|
|
///
|
|
/// ex: true, true, true, true, true, true, true, true -> 0xff
|
|
/// true, false, true, false, false, false, false, false -> 0xa0
|
|
/// false, false, false, false, false, false, false, false -> 0x00
|
|
pub fn bool_to_byte(from: [bool; 8]) -> u8 {
|
|
let mut return_value = 0u8;
|
|
for i in 0..from.len() {
|
|
let new_bit = 0x1 << i;
|
|
if from[i] {
|
|
return_value |= new_bit
|
|
}
|
|
}
|
|
return_value
|
|
}
|
|
|
|
/// swap_endian_u16
|
|
///
|
|
/// Swaps endianness of a u16
|
|
///
|
|
/// ex: 0xabcd -> 0xcdab
|
|
/// 0x00ff -> 0xff00
|
|
pub fn swap_endian_u16(from: u16) -> u16 {
|
|
// shift low to high and high to low
|
|
// merge the bits to a u16
|
|
(from & 0xff) << 8 | (from & 0xff00) >> 8
|
|
}
|
|
|
|
/// Swap endian u32
|
|
///
|
|
/// Swaps endianness of a u32
|
|
///
|
|
/// ex. 0x12345678 -> 0x78563412
|
|
pub fn swap_endian_u32(from: u32) -> u32 {
|
|
let mut bytes = vec![0, 0, 0, 0];
|
|
// mask out what we want...
|
|
bytes[0] = from & 0x000000ff;
|
|
bytes[1] = (from & 0x0000ff00) >> 8;
|
|
bytes[2] = (from & 0x00ff0000) >> 16;
|
|
bytes[3] = (from & 0xff000000) >> 24;
|
|
// ...shift it into the right position
|
|
bytes[0] << 24 | bytes[1] << 16 | bytes[2] << 8 | bytes[3]
|
|
}
|
|
|
|
/// split_bytes_u16
|
|
///
|
|
/// Splits a u16 into its high and low bytes
|
|
///
|
|
/// ex: 0xabcd -> 0xab, 0xcd
|
|
/// 0x0000 -> 0x00, 0x00
|
|
/// 0xffff -> 0xff, 0xff
|
|
pub fn split_bytes_u16(from: u16) -> (u8, u8) {
|
|
let high = from.rotate_left(8) as u8;
|
|
let low = (from & 0xff) as u8;
|
|
|
|
(high, low)
|
|
}
|
|
|
|
/// join_bytes_u16
|
|
pub fn join_bytes_u16(high: u16, low: u16) -> u16 {
|
|
low | high << 8
|
|
}
|
|
|
|
/// combine_u8_to_u16
|
|
///
|
|
/// Combines a pair of u8 values into a single u16 with high
|
|
/// and low bytes specified
|
|
///
|
|
/// ex: (0xff, 0x00) -> 0x00ff
|
|
/// (0x00, 0xff) -> 0xff00
|
|
/// (0xbe, 0xef) -> 0xefbe
|
|
/// (0xef, 0xbe) -> 0xbeef
|
|
pub fn combine_u8_to_u16(low: u8, high: u8) -> u16 {
|
|
(high as u16) << 8 | low as u16
|
|
}
|
|
|
|
/// clear_high_bits
|
|
///
|
|
/// Clear the 8 MSB of the value
|
|
///
|
|
/// ex: 0xffff -> 0x00ff
|
|
/// 0xabcd -> 0x00cd
|
|
pub fn clear_high_bits(to_clear: u16) -> u16 {
|
|
to_clear & 0x00ff
|
|
}
|
|
|
|
/// clear_low_bits
|
|
///
|
|
/// Clear the 8 LSB of the value
|
|
///
|
|
/// ex: 0xffff -> 0xff00
|
|
/// 0xabcd -> 0xab00
|
|
pub fn clear_low_bits(to_clear: u16) -> u16 {
|
|
to_clear & 0xff00
|
|
}
|
|
|
|
/// is_bit_set
|
|
///
|
|
/// Checks if a specified bit is set
|
|
pub fn is_bit_set(to_check: u8, bit_to_check: u8) -> bool {
|
|
(to_check >> bit_to_check) & 0x01 == 1
|
|
}
|
|
|
|
/// is_bit_clear
|
|
///
|
|
/// Checks if a specified bit is clear
|
|
/// LSB = 0 MSB = 7
|
|
pub fn is_bit_clear(to_check: u8, bit_to_check: u8) -> bool {
|
|
(to_check >> bit_to_check) & 0x01 != 1
|
|
}
|
|
}
|
|
|