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 
20 #ifndef BCD_H
21 #define BCD_H
22 
23 #include <stddef.h>
24 #include <stdint.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
37 static inline uint8_t bcd_from_byte(uint8_t byte)
38 {
39  /* ((byte / 10) << 4) | (byte % 10) */
40  return byte + (6 * (byte / 10));
41 }
42 
50 static inline uint8_t bcd_to_byte(uint8_t bcd)
51 {
52  /* == (10 * (bcd >> 4)) + (bcd & 0xf) */
53  return bcd - (6 * (bcd >> 4));
54 }
55 
70 int bcd_buf_from_u32(uint32_t val, void *dst, size_t len);
71 
72 #ifdef __cplusplus
73 }
74 #endif
75 
76 #endif /* BCD_H */
static uint8_t bcd_to_byte(uint8_t bcd)
Converts a binary coded decimal to a byte.
Definition: bcd.h:50
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:37