Add some hex routines to utils.

This commit is contained in:
Arthur Britto
2012-04-29 12:58:23 -07:00
parent 5e97213bb1
commit 79470a297c
2 changed files with 29 additions and 0 deletions

View File

@@ -19,4 +19,9 @@ boost::posix_time::ptime ptFromSeconds(int iSeconds)
: ptEpoch() + boost::posix_time::seconds(iSeconds);
}
char charHex(int iDigit)
{
return iDigit < 10 ? '0' + iDigit : 'A' - 10 + iDigit;
}
// vim:ts=4

View File

@@ -22,6 +22,30 @@ std::string strJoin(Iterator first, Iterator last, std::string strSeperator)
return ossValues.str();
}
char charHex(int iDigit);
template<class Iterator>
void strHex(std::string& strDst, Iterator first, int iSize)
{
strDst.resize(iSize*2);
for (int i = 0; i < iSize; i++) {
unsigned char c = *first++;
strDst[i*2] = charHex(c >> 4);
strDst[i*2+1] = charHex(c & 15);
}
}
inline void strHex(std::string& strDst, const std::string& strSrc) {
strHex(strDst, strSrc.begin(), strSrc.size());
}
inline void strHex(std::string& strDst, const std::vector<unsigned char> vchData) {
strHex(strDst, vchData.begin(), vchData.size());
}
#endif
// vim:ts=4