summaryrefslogtreecommitdiffstats
path: root/src/common/hex.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/hex.cc')
-rw-r--r--src/common/hex.cc35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/common/hex.cc b/src/common/hex.cc
new file mode 100644
index 000000000..a02e0fd68
--- /dev/null
+++ b/src/common/hex.cc
@@ -0,0 +1,35 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2008-2011 New Dream Network
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation. See file COPYING.
+ *
+ */
+
+#include "common/hex.h"
+
+void hex2str(const char *s, int len, char *buf, int dest_len)
+{
+ int pos = 0;
+ for (int i=0; i<len && pos<dest_len; i++) {
+ if (i && !(i%8))
+ pos += snprintf(&buf[pos], dest_len-pos, " ");
+ if (i && !(i%16))
+ pos += snprintf(&buf[pos], dest_len-pos, "\n");
+ pos += snprintf(&buf[pos], dest_len-pos, "%.2x ", (int)(unsigned char)s[i]);
+ }
+}
+
+std::string hexdump(const std::string &msg, const char *s, int len)
+{
+ int buf_len = len*4;
+ char buf[buf_len];
+ hex2str(s, len, buf, buf_len);
+ return buf;
+}