From 653113412fe1099bc80bc128f6501ea77631cc0e Mon Sep 17 00:00:00 2001 From: Pierre Gondois Date: Thu, 30 Sep 2021 08:48:15 +0100 Subject: DynamicTablesPkg: Add HexFromAscii() to AcpiHelperLib Add HexFromAscii(), converting an hexadecimal ascii char to an integer. Reviewed-by: Sami Mujawar Signed-off-by: Pierre Gondois --- DynamicTablesPkg/Include/Library/AcpiHelperLib.h | 15 +++++++++++ .../Library/Common/AcpiHelperLib/AcpiHelper.c | 31 ++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/DynamicTablesPkg/Include/Library/AcpiHelperLib.h b/DynamicTablesPkg/Include/Library/AcpiHelperLib.h index 53ab19b1d1..094392a1a6 100644 --- a/DynamicTablesPkg/Include/Library/AcpiHelperLib.h +++ b/DynamicTablesPkg/Include/Library/AcpiHelperLib.h @@ -34,6 +34,21 @@ AsciiFromHex ( IN UINT8 Hex ); +/** Convert an ASCII char representing an hexadecimal number + to its integer value. + + @param [in] Char Char to convert. + Must be between '0'-'9' or 'A'-'F' or 'a'-'f'. + + @return The corresponding integer (between 0-16). + -1 if error. +**/ +UINT8 +EFIAPI +HexFromAscii ( + IN CHAR8 Char + ); + /** Check if a HID is a valid PNP ID. @param [in] Hid The Hid to validate. diff --git a/DynamicTablesPkg/Library/Common/AcpiHelperLib/AcpiHelper.c b/DynamicTablesPkg/Library/Common/AcpiHelperLib/AcpiHelper.c index 434b472cbb..0b566f0502 100644 --- a/DynamicTablesPkg/Library/Common/AcpiHelperLib/AcpiHelper.c +++ b/DynamicTablesPkg/Library/Common/AcpiHelperLib/AcpiHelper.c @@ -38,6 +38,37 @@ AsciiFromHex ( return (UINT8)-1; } +/** Convert an ASCII char representing an hexadecimal number + to its integer value. + + @param [in] Char Char to convert. + Must be between '0'-'9' or 'A'-'F' or 'a'-'f'. + + @return The corresponding integer (between 0-16). + -1 if error. +**/ +UINT8 +EFIAPI +HexFromAscii ( + IN CHAR8 Char + ) +{ + if ((Char >= '0') && (Char <= '9')) { + return (UINT8)(Char - '0'); + } + + if ((Char >= 'A') && (Char <= 'F')) { + return (UINT8)(Char - 'A' + 10); + } + + if ((Char >= 'a') && (Char <= 'f')) { + return (UINT8)(Char - 'a' + 10); + } + + ASSERT (FALSE); + return (UINT8)-1; +} + /** Check if a HID is a valid PNP ID. @param [in] Hid The Hid to validate. -- cgit v1.2.3