23 if(x >=
'0' && x <=
'9')
return x -
'0';
24 if(x >=
'A' && x <=
'F')
return x -
'A' + 10;
25 if(x >=
'a' && x <=
'f')
return x -
'a' + 10;
26 throw std::invalid_argument(
"Invalid argument");
38 if(x <= 9)
return x +
'0';
39 if(x >= 10 && x <= 15)
return (x - 10) +
'a';
40 throw std::invalid_argument(
"Invalid argument");
52 inline std::size_t
hexStringToBuffer(
const char *input, std::size_t inputLength,
Buffer output,
bool stripZeroes =
false) {
54 while(*input ==
'0') {
59 if(inputLength == 0)
return 0;
61 std::size_t outputLength = (inputLength + 1) / 2;
63 if(inputLength % 2 == 1) {
68 while(inputLength > 0) {
99 if(inputLength == 0)
return 0;
101 Buffer inputEnd = input + inputLength;
102 while(input != inputEnd) {
110 if(nullTerminated) *output =
'\0';
112 return inputLength * 2;
123 std::size_t length = 0;
124 if(x > 0xFFFFFFFFFFFFFF) length = 8;
125 else if(x > 0xFFFFFFFFFFFF) length = 7;
126 else if(x > 0xFFFFFFFFFF) length = 6;
127 else if(x > 0xFFFFFFFF) length = 5;
128 else if(x > 0xFFFFFF) length = 4;
129 else if(x > 0xFFFF) length = 3;
130 else if(x > 0xFF) length = 2;
131 else if(x > 0) length = 1;
137 Buffer outputStart = output + length;
138 for(; x != 0; x >>= 8) *(--outputStart) = x & 0xFF;
Namespace holding all converters and other utilities.
Definition: utils.hpp:10
std::size_t intToBuffer(std::uint64_t x, Buffer output)
Converts integer to buffer.
Definition: utils.hpp:122
std::uint8_t Byte
Definition: utils.hpp:11
std::size_t bufferToHexString(Buffer input, std::size_t inputLength, char *output, bool nullTerminated=false)
Converts buffer to hexadecimal string.
Definition: utils.hpp:98
Byte * Buffer
Definition: utils.hpp:12
std::size_t hexStringToBuffer(const char *input, std::size_t inputLength, Buffer output, bool stripZeroes=false)
Converts hexadecimal string to buffer.
Definition: utils.hpp:52
char byteToHexChar(Byte x)
Converts byte to hexadecimal char.
Definition: utils.hpp:37
Byte hexCharToByte(char x)
Converts hexadecimal char to byte.
Definition: utils.hpp:22