TDME2  1.9.200
Hex.cpp
Go to the documentation of this file.
1 #include <string.h>
2 
3 #include <tdme/tdme.h>
4 #include <tdme/utilities/Hex.h>
5 
6 using std::string;
7 
9 
10 void Hex::encodeInt(const uint64_t decodedInt, string& encodedString) {
11  encodedString = "";
12  char encodingCharSet[] = "0123456789abcdef";
13  auto _decodedInt = decodedInt;
14  for (auto i = 0; i < 32; i++) {
15  auto charIdx = _decodedInt & 15;
16  encodedString = encodingCharSet[charIdx] + encodedString;
17  _decodedInt>>= 4;
18  if (_decodedInt == 0) break;
19  }
20 }
21 
22 bool Hex::decodeInt(const string& encodedString, uint64_t& decodedInt) {
23  char encodingCharSet[] = "0123456789abcdef";
24  decodedInt = 0;
25  for (auto i = 0; i < encodedString.length(); i++) {
26  auto codeIdx = -1;
27  char c = encodedString[encodedString.length() - i - 1];
28  char* codePtr = strchr(encodingCharSet, c);
29  if (codePtr == NULL) {
30  return false;
31  } else {
32  codeIdx = codePtr - encodingCharSet;
33  }
34  decodedInt+= codeIdx << (i * 4);
35  }
36  return true;
37 }
Integer to hex string conversion utility class.
Definition: Hex.h:16
static uint64_t decodeInt(const string &encodedString)
Decodes a hex string representation to an 64 bit unsigned integer.
Definition: Hex.h:35