blob: 7d85b582f5c2f6d434b796b1549fbf00259621e5 [file] [log] [blame]
Lawrence Tangaacf0e22022-07-20 13:28:52 +01001/**
2 * Describes common utility functions shared between CPER projects within this repository.
3 * No functions here depend on json-c or b64.c.
Ed Tanousfedd4572024-07-12 13:56:00 -07004 *
Lawrence Tangaacf0e22022-07-20 13:28:52 +01005 * Author: Lawrence.Tang@arm.com
6 **/
7
Thu Nguyene42fb482024-10-15 14:43:11 +00008#include <libcper/BaseTypes.h>
9#include <libcper/common-utils.h>
Lawrence Tangaacf0e22022-07-20 13:28:52 +010010
11//Converts the given BCD byte to a standard integer.
12int bcd_to_int(UINT8 bcd)
13{
Lawrence Tange407b4c2022-07-21 13:54:01 +010014 return ((bcd & 0xF0) >> 4) * 10 + (bcd & 0x0F);
Lawrence Tangaacf0e22022-07-20 13:28:52 +010015}
16
17//Converts the given integer to a single byte BCD.
18UINT8 int_to_bcd(int value)
19{
Lawrence Tange407b4c2022-07-21 13:54:01 +010020 UINT8 result = 0;
21 int shift = 0;
22 while (value > 0) {
23 result |= (value % 10) << (shift++ << 2);
24 value /= 10;
25 }
Lawrence Tangaacf0e22022-07-20 13:28:52 +010026
Lawrence Tange407b4c2022-07-21 13:54:01 +010027 return result;
John Chungf8fc7052024-05-03 20:05:29 +080028}