Printing the hexadecimal representation of a char array[]

后端 未结 3 1476
盖世英雄少女心
盖世英雄少女心 2020-12-30 08:22

I\'ve got an array of 8 bytes that I\'m trying to print out the hexadecimal notation for. Using printf(\"%x\", array) I can get at the first byte and print it

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-30 08:59

    This is what I did, its a little bit easier with a function and I use for debugging and logging memory.

    void print_hex_memory(void *mem) {
      int i;
      unsigned char *p = (unsigned char *)mem;
      for (i=0;i<128;i++) {
        printf("0x%02x ", p[i]);
        if ((i%16==0) && i)
          printf("\n");
      }
      printf("\n");
    }
    

提交回复
热议问题