parse hex value from string
Stefan Schuermans

Stefan Schuermans commited on 2019-06-09 20:32:19
Showing 2 changed files, with 37 additions and 0 deletions.

... ...
@@ -111,6 +111,35 @@ bool StringParser::uintMin(unsigned int min, unsigned int &uint)
111 111
   return uintNo(uint) && uint >= min;
112 112
 }
113 113
 
114
+/**
115
+ * @brief parse hexadecimal number
116
+ * @param[in] digits number of digits to parse
117
+ * @param[out] uint number parsed from string
118
+ * @return if parsing was successful
119
+ */
120
+bool StringParser::uintHex(unsigned int digits, unsigned int &uint)
121
+{
122
+  uint = 0;
123
+  for (; digits > 0; --digits) {
124
+    if (m_it == m_str.end()) {
125
+      return false;
126
+    }
127
+    char chr = *m_it;
128
+    ++m_it;
129
+    uint <<= 4;
130
+    if (chr >= '0' && chr <= '9') {
131
+      uint |= chr - '0';
132
+    } else if (chr >= 'A' && chr <= 'F') {
133
+      uint |= chr - 'A' + 10;
134
+    } else if (chr >= 'a' && chr <= 'f') {
135
+      uint |= chr - 'a' + 10;
136
+    } else {
137
+      return false;
138
+    }
139
+  }
140
+  return true;
141
+}
142
+
114 143
 /**
115 144
  * @brief parse signed number
116 145
  * @param[out] sint number parsed from string
... ...
@@ -58,6 +58,14 @@ public:
58 58
    */
59 59
   bool uintMin(unsigned int min, unsigned int &uint);
60 60
 
61
+  /**
62
+   * @brief parse hexadecimal number
63
+   * @param[in] digits number of digits to parse
64
+   * @param[out] uint number parsed from string
65
+   * @return if parsing was successful
66
+   */
67
+  bool uintHex(unsigned int digits, unsigned int &uint);
68
+
61 69
   /**
62 70
    * @brief parse signed number
63 71
    * @param[out] sint number parsed from string
64 72