summaryrefslogtreecommitdiffstats
path: root/common/HexValues.cpp
blob: 5eb2e39405decc0410bbf95431043bef79a0e4af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
 * File:	HexValues.cpp
 *
 * Copyright (c) Freescale Semiconductor, Inc. All rights reserved.
 * See included license file for license details.
 */

#include "HexValues.h"

bool isHexDigit(char c)
{
	return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
}

//! \return The integer equivalent to \a c.
//! \retval -1 The character \a c is not a hex character.
uint8_t hexCharToInt(char c)
{
	if (c >= '0' && c <= '9')
		return c - '0';
	else if (c >= 'a' && c <= 'f')
		return c - 'a' + 10;
	else if (c >= 'A' && c <= 'F')
		return c - 'A' + 10;
	else
		return static_cast<uint8_t>(-1);
}

//! \param encodedByte Must point to at least two ASCII hex characters.
//!
uint8_t hexByteToInt(const char * encodedByte)
{
	return (hexCharToInt(encodedByte[0]) << 4) | hexCharToInt(encodedByte[1]);
}