xref: /netbsd-src/external/bsd/elftosb/dist/common/HexValues.cpp (revision 993229b6fea628ff8b1fa09146c80b0cfb2768eb)
1 /*
2  * File:	HexValues.cpp
3  *
4  * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
5  * See included license file for license details.
6  */
7 
8 #include "HexValues.h"
9 
isHexDigit(char c)10 bool isHexDigit(char c)
11 {
12 	return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
13 }
14 
15 //! \return The integer equivalent to \a c.
16 //! \retval -1 The character \a c is not a hex character.
hexCharToInt(char c)17 uint8_t hexCharToInt(char c)
18 {
19 	if (c >= '0' && c <= '9')
20 		return c - '0';
21 	else if (c >= 'a' && c <= 'f')
22 		return c - 'a' + 10;
23 	else if (c >= 'A' && c <= 'F')
24 		return c - 'A' + 10;
25 	else
26 		return static_cast<uint8_t>(-1);
27 }
28 
29 //! \param encodedByte Must point to at least two ASCII hex characters.
30 //!
hexByteToInt(const char * encodedByte)31 uint8_t hexByteToInt(const char * encodedByte)
32 {
33 	return (hexCharToInt(encodedByte[0]) << 4) | hexCharToInt(encodedByte[1]);
34 }
35