Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Describes common utility functions shared between CPER projects within this repository. |
| 3 | * No functions here depend on json-c or b64.c. |
Ed Tanous | fedd457 | 2024-07-12 13:56:00 -0700 | [diff] [blame] | 4 | * |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 5 | * Author: Lawrence.Tang@arm.com |
| 6 | **/ |
| 7 | |
Thu Nguyen | e42fb48 | 2024-10-15 14:43:11 +0000 | [diff] [blame] | 8 | #include <libcper/BaseTypes.h> |
| 9 | #include <libcper/common-utils.h> |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 10 | |
| 11 | //Converts the given BCD byte to a standard integer. |
| 12 | int bcd_to_int(UINT8 bcd) |
| 13 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 14 | return ((bcd & 0xF0) >> 4) * 10 + (bcd & 0x0F); |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 15 | } |
| 16 | |
| 17 | //Converts the given integer to a single byte BCD. |
| 18 | UINT8 int_to_bcd(int value) |
| 19 | { |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 20 | UINT8 result = 0; |
| 21 | int shift = 0; |
| 22 | while (value > 0) { |
| 23 | result |= (value % 10) << (shift++ << 2); |
| 24 | value /= 10; |
| 25 | } |
Lawrence Tang | aacf0e2 | 2022-07-20 13:28:52 +0100 | [diff] [blame] | 26 | |
Lawrence Tang | e407b4c | 2022-07-21 13:54:01 +0100 | [diff] [blame] | 27 | return result; |
John Chung | f8fc705 | 2024-05-03 20:05:29 +0800 | [diff] [blame] | 28 | } |