Loading...
Searching...
No Matches
bcd.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2017 Freie Universität Berlin
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser
5 * General Public License v2.1. See the file LICENSE in the top level
6 * directory for more details.
7 */
8
9#pragma once
10
23
24#include <stdbool.h>
25#include <stddef.h>
26#include <stdint.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
39static inline uint8_t bcd_from_byte(uint8_t byte)
40{
41 /* ((byte / 10) << 4) | (byte % 10) */
42 return byte + (6 * (byte / 10));
43}
44
52static inline uint8_t bcd_to_byte(uint8_t bcd)
53{
54 /* == (10 * (bcd >> 4)) + (bcd & 0xf) */
55 return bcd - (6 * (bcd >> 4));
56}
57
72int bcd_buf_from_u32(uint32_t val, void *dst, size_t len);
73
83uint32_t bcd_buf_to_u32(const void *src, size_t len);
84
93uint64_t bcd_buf_to_u64(const void *src, size_t len);
94
108int bcd_buf_from_str(const char *str, size_t str_len,
109 void *dst, size_t dst_len);
110
111#ifdef __cplusplus
112}
113#endif
114
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:52
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:39
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.