Index: base/string_util.cc |
=================================================================== |
--- base/string_util.cc (revision 8370) |
+++ base/string_util.cc (working copy) |
@@ -1510,3 +1510,32 @@ |
return true; |
} |
+ |
+std::string HexEncode(const void* bytes, size_t size) { |
+ static const char kHexChars[] = { |
Dean McNamee
2009/01/22 11:22:04
Could you use "0123456789ABCDEF" instead?
|
+ '0', '1', '2', '3', '4', '5', '6', '7', |
+ '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; |
+ |
+ if (size == 0) |
+ return std::string(); |
+ |
+ std::string ret; |
Dean McNamee
2009/01/22 11:22:04
Why not:
std::string ret(size * 2, '\0'); or some
|
+ // For each byte, we print two characters. |
+ ret.resize(size * 2); |
+ |
+ const unsigned char* pos = reinterpret_cast<const unsigned char*>(bytes); |
+ const unsigned char* end = pos + size; |
+ std::string::iterator write = ret.begin(); |
+ while (pos < end) { |
+ unsigned char b = *pos; |
+ pos++; |
+ |
+ write[0] = kHexChars[(b >> 4) & 0xf]; |
Dean McNamee
2009/01/22 11:22:04
I find write[0] strange if you're treating it like
|
+ write++; |
+ |
+ write[0] = kHexChars[b & 0xf]; |
+ write++; |
+ } |
+ |
+ return ret; |
+} |