Commit 67459aa2 authored by Jake Swensen's avatar Jake Swensen Committed by Christopher Friedt
Browse files

sys: util: add binary coded decimal functions



Some devices (such as RTCs) have data formats that expect BCD values
instead of binary. These routines allow for converting between binary
and BCD formats.

Signed-off-by: default avatarJake Swensen <jake@swensen.io>
parent 8edcf028
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -285,6 +285,30 @@ size_t bin2hex(const uint8_t *buf, size_t buflen, char *hex, size_t hexlen);
 */
size_t hex2bin(const char *hex, size_t hexlen, uint8_t *buf, size_t buflen);

/**
 * @brief Convert a binary coded decimal (BCD 8421) value to binary.
 *
 * @param bcd BCD 8421 value to convert.
 *
 * @return Binary representation of input value.
 */
static inline uint8_t bcd2bin(uint8_t bcd)
{
	return ((10 * (bcd >> 4)) + (bcd & 0x0F));
}

/**
 * @brief Convert a binary value to binary coded decimal (BCD 8421).
 *
 * @param bin Binary value to convert.
 *
 * @return BCD 8421 representation of input value.
 */
static inline uint8_t bin2bcd(uint8_t bin)
{
	return (((bin / 10) << 4) | (bin % 10));
}

/**
 * @brief      Convert a uint8_t into a decimal string representation.
 *