summaryrefslogtreecommitdiff
path: root/src/mongo/util/hex.cpp
diff options
context:
space:
mode:
authorAndrewCEmil <andrew.emil@10gen.com>2013-04-09 16:58:21 -0700
committerAndrewCEmil <andrew.emil@10gen.com>2013-05-29 11:47:17 -0700
commit135f0fb362efd4be85b422cf386cc26c2cd5d2d5 (patch)
treea60fa72e02c7b128387a6c4227087d9cda2b799d /src/mongo/util/hex.cpp
parent088557a3cf2b1ed0b6656e0fd9812ca5501e723b (diff)
downloadmongo-135f0fb362efd4be85b422cf386cc26c2cd5d2d5.tar.gz
SERVER-7324: added templated toHexTemp function to hex.h/hex.cpp
-replaced incorrect calls of toHex -added test of integerToHex to stringutils_test.cpp -changed building of bsondemo -included hex.cpp to stringutils library package
Diffstat (limited to 'src/mongo/util/hex.cpp')
-rw-r--r--src/mongo/util/hex.cpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/mongo/util/hex.cpp b/src/mongo/util/hex.cpp
new file mode 100644
index 00000000000..ef8cc64a10b
--- /dev/null
+++ b/src/mongo/util/hex.cpp
@@ -0,0 +1,54 @@
+// util/hex.cpp
+
+/* Copyright 2013 10gen Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+#include "mongo/util/hex.h"
+
+namespace mongo {
+
+ template<typename T>
+ std::string integerToHexDef(T inInt) {
+ if(!inInt)
+ return "0";
+
+ static const char hexchars[] = "0123456789ABCDEF";
+
+ static const size_t outbufSize = sizeof(T) * 2 + 1;
+ char outbuf[outbufSize];
+ outbuf[outbufSize - 1] = '\0';
+
+ char c;
+ int lastSeenNumber = 0;
+ for (int j = int(outbufSize) - 2; j >= 0; j--) {
+ c = hexchars[inInt & 0xF];
+ if(c != '0')
+ lastSeenNumber = j;
+ outbuf[j] = c;
+ inInt = inInt >> 4;
+ }
+ char *bufPtr = outbuf;
+ bufPtr += lastSeenNumber;
+
+ return std::string(bufPtr);
+ }
+
+ template<> std::string integerToHex<int>(int val) { return integerToHexDef(val); }
+ template<> std::string integerToHex<unsigned int>(unsigned int val) {
+ return integerToHexDef(val); }
+ template<> std::string integerToHex<long>(long val) { return integerToHexDef(val); }
+ template<> std::string integerToHex<long long>(long long val) { return integerToHexDef(val); }
+}