1. 程式人生 > >打印內存, 打印16進制

打印內存, 打印16進制

log turn urn span rcp ret print name amp

打印內存信息

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

打印內存, 打印16進制