hexDecode helper

This commit is contained in:
Ryan Tharp 2018-06-21 04:13:40 -07:00
parent 2e74bce713
commit 53c5474dc6
2 changed files with 23 additions and 14 deletions

View File

@ -26,6 +26,9 @@ namespace llarp
*ptr = 0;
return &stack[0];
}
int char2int(char input);
void HexDecode(const char* src, uint8_t* target);
}
#endif

View File

@ -1,21 +1,27 @@
#include <llarp/encode.hpp>
#include <stdexcept>
int char2int(char input)
namespace llarp
{
if(input >= '0' && input <= '9')
return input - '0';
if(input >= 'A' && input <= 'F')
return input - 'A' + 10;
if(input >= 'a' && input <= 'f')
return input - 'a' + 10;
throw std::invalid_argument("Invalid input string");
}
void HexDecode(const char* src, uint8_t* target)
{
while(*src && src[1])
int char2int(char input)
{
*(target++) = char2int(*src)*16 + char2int(src[1]);
src += 2;
if(input >= '0' && input <= '9')
return input - '0';
if(input >= 'A' && input <= 'F')
return input - 'A' + 10;
if(input >= 'a' && input <= 'f')
return input - 'a' + 10;
throw std::invalid_argument("Invalid input string");
}
void HexDecode(const char* src, uint8_t* target)
{
while(*src && src[1])
{
*(target++) = char2int(*src)*16 + char2int(src[1]);
src += 2;
}
}
}