Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / common / hex.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2008-2011 New Dream Network
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 #include "common/hex.h"
16
17 void hex2str(const char *s, int len, char *buf, int dest_len)
18 {
19   int pos = 0;
20   for (int i=0; i<len && pos<dest_len; i++) {
21     if (i && !(i%8))
22       pos += snprintf(&buf[pos], dest_len-pos, " ");
23     if (i && !(i%16))
24       pos += snprintf(&buf[pos], dest_len-pos, "\n");
25     pos += snprintf(&buf[pos], dest_len-pos, "%.2x ", (int)(unsigned char)s[i]);
26   }
27 }
28
29 std::string hexdump(std::string msg, const char *s, int len)
30 {
31   int buf_len = len*4;
32   char buf[buf_len];
33   hex2str(s, len, buf, buf_len);
34   return buf;
35 }