打印内存【CSDN】

打印内存【CSDN】打印内存【CSDN】View Code

#include <string.h>
#include <stdio.h>

void printHexString(const void* buf , size_t size);
int main()
{
        char* pstr = new char[64];
        printHexString(pstr , 64);
        const char* ps = "Hello World!";
        printHexString(ps , 20);
        char* pstr2 = new char[64];
        printHexString(pstr2 , 64);
        return 0;
}

void printHexString(const void* buf , size_t size)
{
        char* str = (char*)buf;
        char line[512] = {0};
        const size_t lineLength = 16; // 8或者
        char text[24] = {0};
        char* pc;
        int textLength = lineLength;

        for (size_t ix = 0 ; ix < size ; ix += lineLength)
        {
                sprintf(line, "%.8xh: ", ix);
                // 打印进制
                for (size_t jx = 0 ; jx != lineLength ; jx++)
                {
                        if (ix + jx >= size)
                        {
                                sprintf(line + (11 + jx * 3), "   "); // 处理最后一行空白
                                if (ix + jx == size)
                                        textLength = jx;  // 处理最后一行文本截断
                        } else
                                sprintf(line + (11 + jx * 3), "%.2X ", * (str + ix + jx));
                }
                // 打印字符串
                {
                        memcpy(text, str + ix, lineLength);
                        pc = text;
                        while (pc != text + lineLength)
                        {
                                if ((unsigned char)*pc < 0x20) // 空格之前为控制码
                                        *pc = '.';                 // 控制码转成'.'显示
                                pc++;
                        }
                        text[textLength] = '';
                        sprintf(line + (11 + lineLength * 3), "; %sn", text);
                }
                printf("%s", line);
        }
}

原文链接: https://www.cnblogs.com/guyan/archive/2012/05/14/2500517.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/50241

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午1:50
下一篇 2023年2月9日 上午1:51

相关推荐