pub struct BcdNumber(/* private fields */);Expand description
BcdNumber is a representation of a numeric value encoded in BCD (Binary Coded Decimal) form.
Each byte holds two decimal digits: the upper nibble (4 bits) for the higher digit
and the lower nibble (4 bits) for the lower digit. For example, 0x12 represents the digits “1” and “2”.
The internal byte vector is arranged such that the most significant decimal digits are stored
toward the start of the vector. Thus, BcdNumber(vec![0x12, 0x34]) corresponds to the decimal number “1234”.
This type provides conversions to and from u64, as well as parsing from strings and raw BCD bytes.
Implementations§
Source§impl BcdNumber
impl BcdNumber
Sourcepub fn from_u64(value: u64) -> Self
pub fn from_u64(value: u64) -> Self
Creates a BcdNumber from a u64 value.
Leading zeros are removed except when the value is zero, in which case a single 0x00 byte is used.
§Examples
use bcd_convert::BcdNumber;
let b = BcdNumber::from_u64(1234);
assert_eq!(b.to_string(), "1234");Sourcepub fn from_str_strict(s: &str) -> Result<Self, BcdError>
pub fn from_str_strict(s: &str) -> Result<Self, BcdError>
Creates a BcdNumber from a decimal string.
Leading zeros are removed except when the entire string is zero,
in which case a single zero digit is stored.
Returns Err(BcdError::NonDigitChar(_)) if any non-digit character is found.
§Examples
use bcd_convert::BcdNumber;
let b = BcdNumber::from_str_strict("001234").unwrap();
assert_eq!(b.to_string(), "1234");Sourcepub fn to_u64(&self) -> Result<u64, BcdError>
pub fn to_u64(&self) -> Result<u64, BcdError>
Converts this BcdNumber into a u64.
Returns Err(BcdError::InvalidBcdNibble(_)) if the BCD contains invalid digits.
Returns Err(BcdError::Overflow) if the number cannot fit into a u64.
§Examples
use bcd_convert::BcdNumber;
let b = BcdNumber::from_u64(9999);
let val = b.to_u64().unwrap();
assert_eq!(val, 9999);Sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
Converts the BCD number into a decimal string.
This function assumes the internal representation is always valid BCD, so it never returns an error.
§Examples
use bcd_convert::BcdNumber;
let b = BcdNumber::from_u64(1234);
assert_eq!(b.to_string(), "1234");Sourcepub fn get_digit(&self, index: usize) -> Option<u8>
pub fn get_digit(&self, index: usize) -> Option<u8>
Returns the Nth digit of the decimal number, starting from 0 at the leftmost (most significant) digit.
§Examples
use bcd_convert::BcdNumber;
let b = BcdNumber::from_u64(1234);
assert_eq!(b.get_digit(0), Some(1));
assert_eq!(b.get_digit(1), Some(2));
assert_eq!(b.get_digit(2), Some(3));
assert_eq!(b.get_digit(3), Some(4));
assert_eq!(b.get_digit(4), None);Trait Implementations§
Source§impl Display for BcdNumber
impl Display for BcdNumber
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
Formats the BCD number as a sequence of decimal digits.
This does not remove leading zeros, as BcdNumber maintains a minimal representation internally.
For zero, it will print “00” because a single zero digit is stored as [0x00]. You may handle that case
separately if needed.
Source§impl FromStr for BcdNumber
impl FromStr for BcdNumber
Source§impl TryFrom<&[u8]> for BcdNumber
impl TryFrom<&[u8]> for BcdNumber
Source§fn try_from(value: &[u8]) -> Result<Self, Self::Error>
fn try_from(value: &[u8]) -> Result<Self, Self::Error>
Converts a raw BCD byte slice into a BcdNumber.
This function checks that each nibble is within 0–9.
If invalid data is detected, it returns Err(BcdError::InvalidBcdNibble(_)).
Note: Leading zeros are not stripped here. The provided BCD bytes are stored as is.