打印内存, 打印16进制

打印内存信息

 1 #include <stdio.h>
 2
 3 // 打印内存信息
 4 void showMemoryHex(void* ptr, int size) {
 5     unsigned char* bytes = (unsigned char*)ptr;
 6     for (int i = 0; i < size; i++) {
 7         printf(" %02x", bytes[i]);
 8     }
 9 }

 测试

struct MyStruct {
    int age;
    char name[12];
};

int main(int argc, char* argv[])
{
    MyStruct tony;
    {
        memset(&tony, 0, sizeof(MyStruct));

        tony.age = 123;
        strcpy(tony.name, "tony");
    }


    MyStruct* pTony = &tony;
    showMemoryHex((void*)pTony, sizeof(MyStruct));
    getchar();
    return 0;
}
原文地址:https://www.cnblogs.com/baigoogledu/p/7098253.html