gdb调试MySQL时,查看内存值,很多时候显示为8进制值
利用log协助调试时,log无法直接打印这些8进制的值,调试对比起来很不方便
下面参考了HexDump,实现很简单的OctDump功能,目的是打印格式与gdb调试所显示的格式相同
#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;
std::string ToOct(const std::string& str1)
{
std::string str2;
for (char c : str1)
{
if (std::isprint(c))
{
str2.push_back(c);
}
else
{
str2.push_back('\\');
str2.push_back((char)(((c & 0xc0) >> 6) + '0'));
str2.push_back((char)(((c & 0x38) >> 3) + '0'));
str2.push_back((char)((c & 0x07) + '0'));
}
}
return str2;
}
int main()
{
int v[4] = {1, 20, 3, 9};
std::string str1((char*)(&v[0]), 16);
std::string str2 = ToOct(str1);
cout << "\"" << str2 << "\"" << endl;
const char* cstr = "abc24, #";
char* mstr = (char*)calloc(8, sizeof(char));
memcpy(mstr, cstr, std::min<int>(8, strlen(cstr)));
std::string str3 = ToOct(std::string(mstr, 8));
cout << "\"" << str3 << "\"" << endl;
free(mstr);
}
网友评论