From 5b4f02dfe0bd015808d6ea338d83242c85b3c9bd Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Wed, 14 May 2014 23:09:33 +0900 Subject: [PATCH] src: Rewrite util::format_hex --- src/util.cc | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/util.cc b/src/util.cc index 8b5f418a..59bf12a6 100644 --- a/src/util.cc +++ b/src/util.cc @@ -270,22 +270,20 @@ char upcase(char c) } } +namespace { +const char LOWER_XDIGITS[] = "0123456789abcdef"; +} // namespace + std::string format_hex(const unsigned char *s, size_t len) { std::string res; + res.resize(len * 2); + for(size_t i = 0; i < len; ++i) { - unsigned char c = s[i] >> 4; - if(c > 9) { - res += c - 10 + 'a'; - } else { - res += c + '0'; - } - c = s[i] & 0xf; - if(c > 9) { - res += c - 10 + 'a'; - } else { - res += c + '0'; - } + unsigned char c = s[i]; + + res[i * 2] = LOWER_XDIGITS[c >> 4]; + res[i * 2 + 1] = LOWER_XDIGITS[c & 0x0f]; } return res; }