blob: 38c4317104593263f7abb31426e82f9064959475 [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.
4 *
5 * Author: Lawrence.Tang@arm.com
6 **/
7
8#include "edk/BaseTypes.h"
9#include "common-utils.h"
10
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;
Lawrence Tangaacf0e22022-07-20 13:28:52 +010028}