37 lines
1.0 KiB
Markdown
37 lines
1.0 KiB
Markdown
# nibbles
|
|
|
|
A toy C library that converts numbers to and from [BCD](https://en.wikipedia.org/wiki/Binary-coded_decimal).
|
|
I suggest you don't use it.
|
|
|
|
Named after [half a byte](https://en.wikipedia.org/wiki/Nibble).
|
|
|
|
## Installation
|
|
|
|
Don't. The makefile only includes a test target, because I want to test my code.
|
|
|
|
## Usage
|
|
|
|
The library knows how to convert `uint<n>_t` types into BCD. It also knows
|
|
how to convert them back into `uint64_t`—only that because any other
|
|
representation might lead to a loss of information. It can also convert from
|
|
BCD to strings. The functions are:
|
|
|
|
```c
|
|
bcd bcd_zeros(); // creates a BCD of value 0
|
|
|
|
bcd bcd_from_8(uint8_t); // creates a BCD from a byte
|
|
bcd bcd_from_16(uint16_t); // creates a BCD from 2 bytes
|
|
bcd bcd_from_32(uint32_t); // and so on
|
|
bcd bcd_from_64(uint64_t); // ..
|
|
|
|
uint8_t bcd_digits(bcd); // gets the number of digits in the BCD
|
|
uint64_t bcd_to_64(bcd); // converts a BCD to a 8 bytes (64 bit) number
|
|
char* bcd_to_string(bcd); // converts a BCD to a string (memory is now yours)
|
|
```
|
|
|
|
That's it!
|
|
|
|
<hr/>
|
|
|
|
Have fun!
|