Changed: Centralize hexadecimal conversions
--HG-- branch : develop
This commit is contained in:
parent
b00d62d4ce
commit
9e9b005b3b
4 changed files with 125 additions and 50 deletions
|
@ -233,6 +233,26 @@ char toLower ( const char ch ); // convert only one character
|
||||||
std::string toUpper ( const std::string &str);
|
std::string toUpper ( const std::string &str);
|
||||||
void toUpper ( char *str);
|
void toUpper ( char *str);
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert to an hexadecimal std::string
|
||||||
|
*/
|
||||||
|
std::string toHexa(const uint8 &b);
|
||||||
|
std::string toHexa(const uint8 *data, uint size);
|
||||||
|
std::string toHexa(const std::string &str);
|
||||||
|
std::string toHexa(const char *str);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert from an hexadecimal std::string
|
||||||
|
*/
|
||||||
|
bool fromHexa(const std::string &hexa, uint8 &b);
|
||||||
|
bool fromHexa(const std::string &hexa, uint8 *data);
|
||||||
|
bool fromHexa(const std::string &hexa, std::string &str);
|
||||||
|
bool fromHexa(const char *hexa, uint8 &b);
|
||||||
|
bool fromHexa(const char *hexa, uint8 *data);
|
||||||
|
bool fromHexa(const char *hexa, std::string &str);
|
||||||
|
bool fromHexa(const char hexa, uint8 &b);
|
||||||
|
|
||||||
// Remove all the characters <= 32 (tab, space, new line, return, vertical tab etc..) at the beginning and at the end of a string
|
// Remove all the characters <= 32 (tab, space, new line, return, vertical tab etc..) at the beginning and at the end of a string
|
||||||
template <class T> T trim (const T &str)
|
template <class T> T trim (const T &str)
|
||||||
{
|
{
|
||||||
|
|
|
@ -662,6 +662,109 @@ void toUpper(char *str)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string toHexa(const uint8 &b)
|
||||||
|
{
|
||||||
|
return toString("%02hhx", b);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string toHexa(const uint8 *data, uint size)
|
||||||
|
{
|
||||||
|
std::string res;
|
||||||
|
|
||||||
|
// hexadecimal string will be always twice the original size
|
||||||
|
res.reserve(size * 2);
|
||||||
|
|
||||||
|
// process each byte
|
||||||
|
for (uint i = 0; i < size; ++i)
|
||||||
|
{
|
||||||
|
res += toHexa(data[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string toHexa(const std::string &str)
|
||||||
|
{
|
||||||
|
return toHexa((uint8*)str.c_str(), (uint)str.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string toHexa(const char *str)
|
||||||
|
{
|
||||||
|
return toHexa((uint8*)str, (uint)strlen(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fromHexa(const std::string &hexa, uint8 &b)
|
||||||
|
{
|
||||||
|
return fromHexa(hexa.c_str(), b);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fromHexa(const std::string &hexa, uint8 *data)
|
||||||
|
{
|
||||||
|
return fromHexa(hexa.c_str(), data);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fromHexa(const std::string &hexa, std::string &str)
|
||||||
|
{
|
||||||
|
return fromHexa(hexa.c_str(), str);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fromHexa(const char *hexa, uint8 &b)
|
||||||
|
{
|
||||||
|
char c1 = *hexa;
|
||||||
|
char c2 = *(hexa+1);
|
||||||
|
uint8 x1, x2;
|
||||||
|
if (!fromHexa(c1, x1)) return false;
|
||||||
|
if (!fromHexa(c2, x2)) return false;
|
||||||
|
|
||||||
|
b = (x1 << 4) | x2;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fromHexa(const char *hexa, uint8 *data)
|
||||||
|
{
|
||||||
|
// length of the string
|
||||||
|
uint len = strlen(hexa);
|
||||||
|
|
||||||
|
// process each byte
|
||||||
|
for (uint i = 0; i < len; i += 2)
|
||||||
|
{
|
||||||
|
if (!fromHexa(hexa + i, *(data++))) return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fromHexa(const char *hexa, std::string &str)
|
||||||
|
{
|
||||||
|
str.resize(strlen(hexa) * 2);
|
||||||
|
|
||||||
|
return fromHexa(hexa, (uint8*)str.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
bool fromHexa(const char hexa, uint8 &b)
|
||||||
|
{
|
||||||
|
if (hexa >= '0' && hexa <= '9')
|
||||||
|
{
|
||||||
|
b = hexa - '0';
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hexa >= 'A' && hexa <= 'F')
|
||||||
|
{
|
||||||
|
b = hexa - 'A' + 10;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hexa >= 'a' && hexa <= 'f')
|
||||||
|
{
|
||||||
|
b = hexa - 'a' + 10;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
std::string formatThousands(const std::string& s)
|
std::string formatThousands(const std::string& s)
|
||||||
{
|
{
|
||||||
sint i, k;
|
sint i, k;
|
||||||
|
|
|
@ -118,31 +118,6 @@ CHashKeyMD5 getMD5(const uint8 *buffer, uint32 size)
|
||||||
return Message_Digest;
|
return Message_Digest;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ****************************************************************************
|
|
||||||
// Helper
|
|
||||||
// ****************************************************************************
|
|
||||||
static bool fromHex(char c, uint8 &x)
|
|
||||||
{
|
|
||||||
if (c >= '0' && c <= '9')
|
|
||||||
{
|
|
||||||
x = c - '0';
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (c >= 'A' && c <= 'F')
|
|
||||||
{
|
|
||||||
x = c - 'A' + 10;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
else if (c >= 'a' && c <= 'f')
|
|
||||||
{
|
|
||||||
x = c - 'a' + 10;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
nlwarning("cannot convert to hexa");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ****************************************************************************
|
// ****************************************************************************
|
||||||
// ****************************************************************************
|
// ****************************************************************************
|
||||||
// CHashKeyMD5
|
// CHashKeyMD5
|
||||||
|
@ -159,10 +134,7 @@ void CHashKeyMD5::clear()
|
||||||
// ****************************************************************************
|
// ****************************************************************************
|
||||||
string CHashKeyMD5::toString() const
|
string CHashKeyMD5::toString() const
|
||||||
{
|
{
|
||||||
string sTmp;
|
return toHexa(Data, 16);
|
||||||
for (uint32 i = 0; i < 16; ++i)
|
|
||||||
sTmp += NLMISC::toString("%02x", Data[i]);
|
|
||||||
return sTmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ****************************************************************************
|
// ****************************************************************************
|
||||||
|
@ -174,16 +146,7 @@ bool CHashKeyMD5::fromString(const std::string &in)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint32 i = 0; i < 16; ++i)
|
return fromHexa(in, Data);
|
||||||
{
|
|
||||||
char c1 = in[2*i];
|
|
||||||
char c2 = in[2*i+1];
|
|
||||||
uint8 x1, x2;
|
|
||||||
if (!fromHex(c1, x1)) return false;
|
|
||||||
if (!fromHex(c2, x2)) return false;
|
|
||||||
Data[i] = (x1 << 4) | x2;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ****************************************************************************
|
// ****************************************************************************
|
||||||
|
|
|
@ -560,17 +560,6 @@ inline CLFECOMMON::TCoord getAbsoluteCoordinateFrom64( uint64 posvalue )
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
|
|
||||||
inline std::string toHexaString(const std::vector<uint8> &v)
|
|
||||||
{
|
|
||||||
std::string res;
|
|
||||||
for (uint i = 0; i < v.size(); i++)
|
|
||||||
{
|
|
||||||
res += NLMISC::toString("%x",v[i]);
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
#endif // NL_ENTITY_TYPES_H
|
#endif // NL_ENTITY_TYPES_H
|
||||||
|
|
||||||
/* End of entity_types.h */
|
/* End of entity_types.h */
|
||||||
|
|
Loading…
Reference in a new issue