HexHelpers.h
Go to the documentation of this file.
1 #pragma once
2 #include <bzscore/string.h>
3 
4 namespace GDBServerFoundation
5 {
7  namespace HexHelpers
8  {
9  const char hexTable[17] = "0123456789abcdef";
10 
12  static inline unsigned hexToInt(char hexChar)
13  {
14  //TODO: convert to a table-based version
15  if (hexChar >= '0' && hexChar <= '9')
16  return hexChar - '0';
17  if (hexChar >= 'A' && hexChar <= 'F')
18  return hexChar + 0x0A - 'A';
19  if (hexChar >= 'a' && hexChar <= 'f')
20  return hexChar + 0x0A - 'a';
21  return 0;
22  }
23 
25  static inline unsigned ParseHexValue(const char *p)
26  {
27  return (hexToInt(p[0]) << 4) | hexToInt(p[1]);
28  }
29 
31  static inline unsigned ParseHexValue(char firstChar, char secondChar)
32  {
33  return (hexToInt(firstChar) << 4) | hexToInt(secondChar);
34  }
35 
37  template<typename _Type> static inline _Type ParseHexString(const BazisLib::TempStringA &str)
38  {
39  _Type result = 0;
40  size_t i = 0;
41  bool negate = false;
42  if (!str.empty() && str[0] == '-')
43  negate = true, i++;
44 
45  for (;i < str.size(); i++)
46  result = (result << 4) | hexToInt(str[i]);
47 
48  if (negate)
49  return 0 - result;
50  return result;
51  }
52  }
53 }