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);
    }
}

转载于:https://www.cnblogs.com/cheungxiongwei/p/7839176.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值