diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2012-04-21 20:15:25 +0200 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2012-04-21 20:37:50 +0200 |
commit | 88dc2d6c6a4a71297c30339fc62ce60e53df5dc8 (patch) | |
tree | 16a02f315f0224724d67ca324e414557d121af96 /src/util.h | |
parent | 00b9c0f4b20f6eb714fa55eb00df326a6f74fd10 (diff) |
Integrate @JoelKatz's optimized ToHex (#562) into current HexStr function
Diffstat (limited to 'src/util.h')
-rw-r--r-- | src/util.h | 23 |
1 files changed, 14 insertions, 9 deletions
diff --git a/src/util.h b/src/util.h index d9ec68c620..fe8ca60b47 100644 --- a/src/util.h +++ b/src/util.h @@ -361,15 +361,20 @@ inline int64 abs64(int64 n) template<typename T> std::string HexStr(const T itbegin, const T itend, bool fSpaces=false) { - if (itbegin == itend) - return ""; - const unsigned char* pbegin = (const unsigned char*)&itbegin[0]; - const unsigned char* pend = pbegin + (itend - itbegin) * sizeof(itbegin[0]); - std::string str; - str.reserve((pend-pbegin) * (fSpaces ? 3 : 2)); - for (const unsigned char* p = pbegin; p != pend; p++) - str += strprintf((fSpaces && p != pend-1 ? "%02x " : "%02x"), *p); - return str; + std::vector<char> rv; + static char hexmap[16] = { '0', '1', '2', '3', '4', '5', '6', '7', + '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + rv.reserve((itend-itbegin)*3); + for(T it = itbegin; it < itend; ++it) + { + unsigned char val = (unsigned char)(*it); + if(fSpaces && it != itbegin) + rv.push_back(' '); + rv.push_back(hexmap[val>>4]); + rv.push_back(hexmap[val&15]); + } + + return std::string(rv.begin(), rv.end()); } inline std::string HexStr(const std::vector<unsigned char>& vch, bool fSpaces=false) |