Add fromHex() static function to ByteArray
This commit is contained in:
parent
7f5b787cb9
commit
7084fb7025
|
@ -104,6 +104,13 @@ namespace gourou
|
||||||
*/
|
*/
|
||||||
std::string toBase64();
|
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
|
* @brief Return a string with human readable hex encoded internal data
|
||||||
*/
|
*/
|
||||||
|
@ -130,7 +137,7 @@ namespace gourou
|
||||||
void append(const std::string& str);
|
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;}
|
unsigned char* data() {return _data;}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
along with libgourou. If not, see <http://www.gnu.org/licenses/>.
|
along with libgourou. If not, see <http://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdexcept>
|
||||||
|
|
||||||
#include <Base64.h>
|
#include <Base64.h>
|
||||||
|
|
||||||
|
@ -155,6 +156,47 @@ namespace gourou
|
||||||
return macaron::Base64::Encode(std::string((char*)_data, _length));
|
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()
|
std::string ByteArray::toHex()
|
||||||
{
|
{
|
||||||
char* tmp = new char[_length*2+1];
|
char* tmp = new char[_length*2+1];
|
||||||
|
|
Loading…
Reference in New Issue
Block a user