diff options
author | tommi@chromium.org <tommi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-22 00:10:08 +0000 |
---|---|---|
committer | tommi@chromium.org <tommi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-22 00:10:08 +0000 |
commit | 20d95e085600c007a6faac7dec0da0f8e9e45cd2 (patch) | |
tree | f8b760b9741872d0f44d3a7c351211c43d714fe4 /base/string_util.cc | |
parent | c71b654ff294d084ab7e3f24559867cf729f37b8 (diff) | |
download | chromium_src-20d95e085600c007a6faac7dec0da0f8e9e45cd2.zip chromium_src-20d95e085600c007a6faac7dec0da0f8e9e45cd2.tar.gz chromium_src-20d95e085600c007a6faac7dec0da0f8e9e45cd2.tar.bz2 |
Adding a HexEncode function to string_utils.
This function takes a pointer to a chunk of memory and formats the bytes as hex.
Review URL: http://codereview.chromium.org/18452
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8420 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util.cc')
-rw-r--r-- | base/string_util.cc | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/base/string_util.cc b/base/string_util.cc index eae60d2..45a4442 100644 --- a/base/string_util.cc +++ b/base/string_util.cc @@ -1532,3 +1532,32 @@ bool ElideString(const std::wstring& input, int max_len, std::wstring* output) { return true; } + +std::string HexEncode(const void* bytes, size_t size) { + static const char kHexChars[] = { + '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; + // 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]; + write++; + + write[0] = kHexChars[b & 0xf]; + write++; + } + + return ret; +} |