Loading...
Searching...
No Matches
bcd.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Freie Universität Berlin
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
20
21#include <stdbool.h>
22#include <stddef.h>
23#include <stdint.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
36static inline uint8_t bcd_from_byte(uint8_t byte)
37{
38 /* ((byte / 10) << 4) | (byte % 10) */
39 return byte + (6 * (byte / 10));
40}
41
49static inline uint8_t bcd_to_byte(uint8_t bcd)
50{
51 /* == (10 * (bcd >> 4)) + (bcd & 0xf) */
52 return bcd - (6 * (bcd >> 4));
53}
54
69int bcd_buf_from_u32(uint32_t val, void *dst, size_t len);
70
80uint32_t bcd_buf_to_u32(const void *src, size_t len);
81
90uint64_t bcd_buf_to_u64(const void *src, size_t len);
91
105int bcd_buf_from_str(const char *str, size_t str_len,
106 void *dst, size_t dst_len);
107
108#ifdef __cplusplus
109}
110#endif
111
uint32_t bcd_buf_to_u32(const void *src, size_t len)
Convert a BCD buffer into it's binary representation (This will reverse bcd_buf_from_u32)
static uint8_t bcd_to_byte(uint8_t bcd)
Converts a binary coded decimal to a byte.
Definition bcd.h:49
int bcd_buf_from_u32(uint32_t val, void *dst, size_t len)
Convert a decimal value into a BCD buffer.
static uint8_t bcd_from_byte(uint8_t byte)
Converts a byte to a binary coded decimal.
Definition bcd.h:36
int bcd_buf_from_str(const char *str, size_t str_len, void *dst, size_t dst_len)
Convert a string into a BCD buffer Digits may be separated by any character.
uint64_t bcd_buf_to_u64(const void *src, size_t len)
Convert a BCD buffer into it's binary representation.