HexUtils.cpp
Go to the documentation of this file.
1 #include <NovaDAQUtilities/HexUtils.h>
2 #include <boost/algorithm/string.hpp>
3 #include <boost/lexical_cast.hpp>
4 #include <sstream>
5 #include <iomanip>
6 
7 namespace novadaq {
8 namespace HexUtils {
9 
10 /**
11  * Formats the specified 32-bit integer as a hex string with
12  * the specified number of hex digits.
13  */
14 std::string format(const int32_t& value, const int& ndigits)
15 {
16  std::ostringstream formatter;
17  formatter << "0x" << std::hex << std::setfill('0')
18  << std::setw(ndigits) << std::uppercase << value;
19  return formatter.str();
20 }
21 
22 /**
23  * Formats the specified 32-bit integer as a hex string with
24  * the specified number of hex digits.
25  */
26 std::string format(const int64_t& value, const int& ndigits)
27 {
28  std::ostringstream formatter;
29  formatter << "0x" << std::hex << std::setfill('0')
30  << std::setw(ndigits) << std::uppercase << value;
31  return formatter.str();
32 }
33 
34 /**
35  * Reformats the specified decimal number string into a hex string
36  * with the specified number of hex digits. (The maximum number
37  * of supported hex digits is 16, corresponding to a 64-bit number.)
38  *
39  * @throws an exception of the parsing of the decimalString into
40  * an integer fails.
41  */
42 std::string format(const std::string& decimalString, const int& ndigits)
43 {
44  if (ndigits > 8) {
45  int64_t value = boost::lexical_cast<int64_t>(decimalString);
46  return format(value, ndigits);
47  }
48  else {
49  int32_t value = boost::lexical_cast<int32_t>(decimalString);
50  return format(value, ndigits);
51  }
52 }
53 
54 /**
55  * Parses the specified string into a 64-bit decimal value,
56  * assuming that the string contains a numeric value in hex,
57  * but does not do any checking that the hex string is valid.
58  *
59  * NOTE that if the input string is not valid hex, the resulting
60  * integer value will essentially be a random number.
61  */
62 int64_t unsafeParse(const std::string& hexString)
63 {
64  std::istringstream iss(hexString);
65  uint64_t u64Value;
66  iss >> std::hex >> u64Value;
67  return (int64_t) u64Value;
68 }
69 
70 /**
71  * Tests whether the specified string contains a valid number
72  * in hex format.
73  */
74 bool isValidHex(const std::string& hexString)
75 {
76  // the logic used in this routine is to parse the input
77  // value, regenerate the hex string, and verify that the
78  // result matches what was input.
79 
80  std::string inputString = boost::trim_copy(hexString);
81  boost::to_upper(inputString);
82  if (inputString.find("0X") != 0) {return false;}
83 
84  int numberOfHexDigits = inputString.length() - 2;
85  int64_t value = unsafeParse(inputString);
86  std::string resultString = format(value, numberOfHexDigits);
87  boost::to_upper(resultString);
88 
89  return (resultString == inputString);
90 }
91 
92 /**
93  * Parses the specified hex string into a 32-bit decimal value.
94  * Returns true if the parsing succeeded, and false if not.
95  */
96 bool parse(const std::string& hexString, int32_t& decimalValue)
97 {
98  if (! isValidHex(hexString)) {return false;}
99  if (hexString.length() > 10) {return false;}
100 
101  std::istringstream iss(hexString);
102  uint32_t u32Value;
103  iss >> std::hex >> u32Value;
104  decimalValue = (int32_t) u32Value;
105  return true;
106 }
107 
108 /**
109  * Parses the specified hex string into a 64-bit decimal value.
110  * Returns true if the parsing succeeded, and false if not.
111  */
112 bool parse(const std::string& hexString, int64_t& decimalValue)
113 {
114  if (! isValidHex(hexString)) {return false;}
115  if (hexString.length() > 18) {return false;}
116 
117  std::istringstream iss(hexString);
118  uint64_t u64Value;
119  iss >> std::hex >> u64Value;
120  decimalValue = (int64_t) u64Value;
121  return true;
122 }
123 
124 } // end of namespace HexUtils
125 } // end of namespace novadaq
std::string trim_copy(std::string source, std::string const &t=" ")
Definition: trim.h:66
bool isValidHex(const std::string &hexString)
Definition: HexUtils.cpp:74
const XML_Char int const XML_Char * value
Definition: expat.h:331
std::string format(const int32_t &value, const int &ndigits=8)
Definition: HexUtils.cpp:14
int64_t unsafeParse(const std::string &hexString)
Definition: HexUtils.cpp:62
bool parse(const std::string &hexString, int32_t &decimalValue)
Definition: HexUtils.cpp:96
enum BeamMode string