CatapultServer  v0.5.0.1 (Elephant)
HexParser.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "ByteArray.h"
23 #include "catapult/exceptions.h"
24 
25 namespace catapult { namespace utils {
26 
28  uint8_t ParseByte(char ch1, char ch2);
29 
31  bool TryParseByte(char ch1, char ch2, uint8_t& by);
32 
34  template<typename TContainer>
35  bool TryParseHexStringIntoContainer(const char* const pHexData, size_t dataSize, TContainer& outputContainer) {
36  if (2 * outputContainer.size() != dataSize)
37  return false;
38 
39  for (auto i = 0u; i < dataSize; i += 2) {
40  if (!TryParseByte(pHexData[i], pHexData[i + 1], outputContainer[i / 2]))
41  return false;
42  }
43 
44  return true;
45  }
46 
48  template<typename TContainer>
49  void ParseHexStringIntoContainer(const char* const pHexData, size_t dataSize, TContainer& outputContainer) {
50  // do not implement in terms of TryParseHexStringIntoContainer in order to provide better exception messages
51  if (2 * outputContainer.size() != dataSize)
52  CATAPULT_THROW_INVALID_ARGUMENT_1("hex string has unexpected size", dataSize);
53 
54  for (auto i = 0u; i < dataSize; i += 2)
55  outputContainer[i / 2] = ParseByte(pHexData[i], pHexData[i + 1]);
56  }
57 
59  template<typename TByteArray>
60  TByteArray ParseByteArray(const std::string& hexString) {
61  TByteArray array;
62  utils::ParseHexStringIntoContainer(hexString.c_str(), hexString.size(), array);
63  return array;
64  }
65 }}
catapult::utils::ParseByte
uint8_t ParseByte(char ch1, char ch2)
Parses two characters (ch1 and ch2) into a byte.
Definition: HexParser.cpp:41
exceptions.h
catapult::utils::ParseByteArray
TByteArray ParseByteArray(const std::string &hexString)
Parses a hex string (hexString) into a byte array.
Definition: HexParser.h:60
catapult::utils::TryParseByte
bool TryParseByte(char ch1, char ch2, uint8_t &by)
Tries to parse two characters (ch1 and ch2) into a byte (by).
Definition: HexParser.cpp:51
CATAPULT_THROW_INVALID_ARGUMENT_1
#define CATAPULT_THROW_INVALID_ARGUMENT_1(MESSAGE, PARAM1)
Macro used to throw a catapult invalid argument with a single parameter.
Definition: exceptions.h:183
ByteArray.h
catapult::utils::TryParseHexStringIntoContainer
bool TryParseHexStringIntoContainer(const char *const pHexData, size_t dataSize, TContainer &outputContainer)
Tries to parse a hex string (pHexData with size dataSize) into outputContainer.
Definition: HexParser.h:35
catapult::utils::ParseHexStringIntoContainer
void ParseHexStringIntoContainer(const char *const pHexData, size_t dataSize, TContainer &outputContainer)
Parses a hex string (pHexData with size dataSize) into outputContainer.
Definition: HexParser.h:49
catapult
Definition: AddressExtractionExtension.cpp:28
HexParser.h