C++ stl 怎么打印内存内容?

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
std::string HexDump(char *buf, int len) {
	std::stringstream save;
	for (unsigned char i = 0;i< len;i++)
	{
		//44 03 00 d1 56 00 00 05 00 16 60 4a 38 08 81 8e
		//44 03 00 D1 56 00 00 05 00 16 60 4A 38 08 81 8E
		save << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << ((*(buf + i)) & 0xff);	
	}
	std::cout << save.str() << std::endl;
	return save.str();
}

VC 打印调试消息

#include <iostream>
#include <string>
#include <sstream>
#include <windows.h>
int main()
{
	std::stringstream save;
	save << (void*)(new string()) << std::endl;
	::OutputDebugStringA(save.str().c_str());
	getchar();
    return 0;
}
void HexDump(char *buf, int len, int addr)
{
	int i, j, k;
	char binstr[80];

	for (i = 0;i<len;i++) 
	{
		if (0 == (i % 16)) 
		{
			sprintf(binstr, "%08x -", i + addr);
			sprintf(binstr, "%s %02x", binstr, (unsigned char)buf[i]);
		}
		else if (15 == (i % 16)) 
		{
			sprintf(binstr, "%s %02x", binstr, (unsigned char)buf[i]);
			sprintf(binstr, "%s  ", binstr);
			for (j = i - 15;j <= i;j++) 
			{
				sprintf(binstr, "%s%c", binstr, ('!'<buf[j] && buf[j] <= '~') ? buf[j] : '.');
			}
			printf("%s\n", binstr);
		}
		else 
		{
			sprintf(binstr, "%s %02x", binstr, (unsigned char)buf[i]);
		}
	}
	if (0 != (i % 16)) 
	{
		k = 16 - (i % 16);
		for (j = 0;j<k;j++) 
		{
			sprintf(binstr, "%s   ", binstr);
		}
		sprintf(binstr, "%s  ", binstr);
		k = 16 - k;
		for (j = i - k;j<i;j++) 
		{
			sprintf(binstr, "%s%c", binstr, ('!'<buf[j] && buf[j] <= '~') ? buf[j] : '.');
		}
		printf("%s\n", binstr);
	}
}
posted @ 2017-11-15 15:26  學海無涯  阅读(1437)  评论(0编辑  收藏  举报