Add fromHex() static function to ByteArray

This commit is contained in:
Grégory Soutadé 2022-08-27 15:40:32 +02:00
parent 7f5b787cb9
commit 7084fb7025
2 changed files with 50 additions and 1 deletions

View File

@ -104,6 +104,13 @@ namespace gourou
*/
std::string toBase64();
/**
* @brief Convert hex string into bytes
*
* @param str Hex string
*/
static ByteArray fromHex(const std::string& str);
/**
* @brief Return a string with human readable hex encoded internal data
*/
@ -130,7 +137,7 @@ namespace gourou
void append(const std::string& str);
/**
* @brief Get internal data. Must bot be freed
* @brief Get internal data. Must not be freed
*/
unsigned char* data() {return _data;}

View File

@ -17,6 +17,7 @@
along with libgourou. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <stdexcept>
#include <Base64.h>
@ -155,6 +156,47 @@ namespace gourou
return macaron::Base64::Encode(std::string((char*)_data, _length));
}
ByteArray ByteArray::fromHex(const std::string& str)
{
if (str.size() % 2)
throw std::invalid_argument("Size of hex string not multiple of 2");
ByteArray res((unsigned int)(str.size()/2));
unsigned int i;
unsigned char* data = res.data();
unsigned char cur, tmp;
for (i=0; i<str.size(); i+=2)
{
cur = 0;
tmp = str[i];
if (tmp >= 'a' && tmp <= 'f')
cur = (tmp - 'a' + 10) << 4;
else if (tmp >= 'A' && tmp <= 'F')
cur = (tmp - 'A' + 10) << 4;
else if (tmp >= '0' && tmp <= '9')
cur = (tmp - '0') << 4;
else
throw std::invalid_argument("Invalid character in hex string");
tmp = str[i+1];
if (tmp >= 'a' && tmp <= 'f')
cur += tmp - 'a' + 10;
else if (tmp >= 'A' && tmp <= 'F')
cur += tmp - 'A' + 10;
else if (tmp >= '0' && tmp <= '9')
cur += tmp - '0';
else
throw std::invalid_argument("Invalid character in hex string");
data[i/2] = cur;
}
return res;
}
std::string ByteArray::toHex()
{
char* tmp = new char[_length*2+1];