mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-16 18:13:49 +01:00
sys/bcd: add bcd_buf_from_u32()
This commit is contained in:
parent
a104c3f3e0
commit
284845e6ea
1
sys/bcd/Makefile
Normal file
1
sys/bcd/Makefile
Normal file
@ -0,0 +1 @@
|
|||||||
|
include $(RIOTBASE)/Makefile.base
|
||||||
58
sys/bcd/bcd.c
Normal file
58
sys/bcd/bcd.c
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2025 ML!PA Consulting GmbH
|
||||||
|
*
|
||||||
|
* This file is subject to the terms and conditions of the GNU Lesser General
|
||||||
|
* Public License v2.1. See the file LICENSE in the top level directory for more
|
||||||
|
* details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ingroup sys_bcd
|
||||||
|
* @{
|
||||||
|
*
|
||||||
|
* @file
|
||||||
|
* @brief Library to de- and encode binary coded decimals
|
||||||
|
*
|
||||||
|
* @author Benjamin Valentin <benjamin.valentin@ml-pa.com>
|
||||||
|
*
|
||||||
|
* @}
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "bcd.h"
|
||||||
|
#include <errno.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static inline uint8_t swap_nibbles(uint8_t b)
|
||||||
|
{
|
||||||
|
return (b << 4) | (b >> 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
int bcd_buf_from_u32(uint32_t val, void *dst, size_t len)
|
||||||
|
{
|
||||||
|
uint8_t *tgt = dst;
|
||||||
|
uint8_t hex = 0;
|
||||||
|
uint8_t idx = 0;
|
||||||
|
|
||||||
|
memset(dst, 0, len);
|
||||||
|
len *= 2;
|
||||||
|
|
||||||
|
do {
|
||||||
|
hex <<= 4;
|
||||||
|
hex += val % 10;
|
||||||
|
val /= 10;
|
||||||
|
if (++idx % 2 == 0) {
|
||||||
|
*tgt++ = swap_nibbles(hex);
|
||||||
|
hex = 0;
|
||||||
|
}
|
||||||
|
} while (val && idx <= len);
|
||||||
|
|
||||||
|
if (idx > len) {
|
||||||
|
return -ENOBUFS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (idx % 2) {
|
||||||
|
*tgt++ = hex;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (uintptr_t)tgt - (uintptr_t)dst;
|
||||||
|
}
|
||||||
@ -20,6 +20,7 @@
|
|||||||
#ifndef BCD_H
|
#ifndef BCD_H
|
||||||
#define BCD_H
|
#define BCD_H
|
||||||
|
|
||||||
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@ -52,6 +53,22 @@ static inline uint8_t bcd_to_byte(uint8_t bcd)
|
|||||||
return bcd - (6 * (bcd >> 4));
|
return bcd - (6 * (bcd >> 4));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Convert a decimal value into a BCD buffer.
|
||||||
|
* (This looks like the decimal integer value when printed as hex)
|
||||||
|
*
|
||||||
|
* This will e.g. turn the value 123 -> 0x123 (decimal: 291)
|
||||||
|
*
|
||||||
|
* @param[in] val Decimal value to print
|
||||||
|
* @param[out] dst Destination buffer
|
||||||
|
* @param[in] len Size of the destination buffer
|
||||||
|
*
|
||||||
|
* @return number of bytes written
|
||||||
|
* @return -ENOBUFS if @p dst is not large enough
|
||||||
|
* In that case the state of @p dst is undefined.
|
||||||
|
*/
|
||||||
|
int bcd_buf_from_u32(uint32_t val, void *dst, size_t len);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user