打印内存, 打印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;
}

 

posted @ 2017-06-30 12:07  菩提树~今生  阅读(799)  评论(0编辑  收藏  举报