bcd.c 630 B

1234567891011121314151617181920212223242526
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only
  5. */
  6. #include <stdint.h>
  7. uint8_t bcd_first_digit_value_bin(uint8_t bcd_value) {
  8. return (((bcd_value & 0xF0) >> 4)) * 10;
  9. }
  10. uint8_t bcd_last_digit_value_bin(uint8_t bcd_value) {
  11. return bcd_value & 0x0F;
  12. }
  13. uint8_t bcd2bin(uint8_t bcd_value) {
  14. return bcd_last_digit_value_bin(bcd_value) + bcd_first_digit_value_bin(bcd_value);
  15. }
  16. uint8_t bin2bcd(uint8_t bin_value) {
  17. uint8_t second_digit = (bin_value / 10) << 4;
  18. uint8_t first_digit = bin_value % 10;
  19. return first_digit + second_digit;
  20. }