Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2947)

Unified Diff: base/string_util.cc

Issue 18452: Adding a HexEncode function to string_utils.... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « base/string_util.h ('k') | base/string_util_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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;
+}
« no previous file with comments | « base/string_util.h ('k') | base/string_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698