From 1e91dfe9d640dde6522ff301f0e4639047a68762 Mon Sep 17 00:00:00 2001 From: murata Date: Wed, 3 Nov 2021 09:09:08 +0900 Subject: [PATCH] AP_Common: Make the char_to_hex method a common method --- libraries/AP_Common/AP_Common.cpp | 16 ++++++++++++++++ libraries/AP_Common/AP_Common.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/libraries/AP_Common/AP_Common.cpp b/libraries/AP_Common/AP_Common.cpp index 3d749353fb..cfc2e5379a 100644 --- a/libraries/AP_Common/AP_Common.cpp +++ b/libraries/AP_Common/AP_Common.cpp @@ -83,3 +83,19 @@ void strncpy_noterm(char *dest, const char *src, size_t n) } memcpy(dest, src, len); } + +/** + * return the numeric value of an ascii hex character + * + * @param[in] a Hexadecimal character + * @return Returns a binary value + */ +int16_t char_to_hex(char a) +{ + if (a >= 'A' && a <= 'F') + return a - 'A' + 10; + else if (a >= 'a' && a <= 'f') + return a - 'a' + 10; + else + return a - '0'; +} diff --git a/libraries/AP_Common/AP_Common.h b/libraries/AP_Common/AP_Common.h index fe36858431..7d96284239 100644 --- a/libraries/AP_Common/AP_Common.h +++ b/libraries/AP_Common/AP_Common.h @@ -149,6 +149,9 @@ bool hex_to_uint8(uint8_t a, uint8_t &res); // return the uint8 value of an asc */ void strncpy_noterm(char *dest, const char *src, size_t n); +// return the numeric value of an ascii hex character +int16_t char_to_hex(char a); + /* Bit manipulation */