BcdNumber

Struct BcdNumber 

Source
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

Source

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");
Source

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");
Source

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);
Source

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");
Source

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 Clone for BcdNumber

Source§

fn clone(&self) -> BcdNumber

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BcdNumber

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for BcdNumber

Source§

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 From<u64> for BcdNumber

Source§

fn from(value: u64) -> Self

Converts a u64 into a BcdNumber.

Source§

impl FromStr for BcdNumber

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a BcdNumber from a string slice containing decimal digits.

Leading zeros are removed except when all digits are zero.

§Errors

Returns Err(BcdError::NonDigitChar(_)) if any character is not a decimal digit.

Source§

type Err = BcdError

The associated error which can be returned from parsing.
Source§

impl PartialEq for BcdNumber

Source§

fn eq(&self, other: &BcdNumber) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<&[u8]> for BcdNumber

Source§

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.

Source§

type Error = BcdError

The type returned in the event of a conversion error.
Source§

impl TryFrom<BcdNumber> for u64

Source§

fn try_from(value: BcdNumber) -> Result<Self, Self::Error>

Attempts to convert a BcdNumber into a u64.

Returns Err(BcdError::InvalidBcdNibble(_)) if invalid digits are found, or Err(BcdError::Overflow) if the number is too large for u64.

Source§

type Error = BcdError

The type returned in the event of a conversion error.
Source§

impl Eq for BcdNumber

Source§

impl StructuralPartialEq for BcdNumber

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.