Printing the hexadecimal representation of a char array[]

后端 未结 3 1463
盖世英雄少女心
盖世英雄少女心 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条回答
  •  萌比男神i
    2020-12-30 08:42

    Print a string in hex:

    void print_hex(const char *string)
    {
            unsigned char *p = (unsigned char *) string;
    
            for (int i=0; i < strlen(string); ++i) {
                    if (! (i % 16) && i)
                            printf("\n");
    
                    printf("0x%02x ", p[i]);
            }
            printf("\n\n");
    }
    
    char *umlauts = "1 ä ö ü  Ä Ö Ü  é É  ß € 2";
    
    print_hex(umlauts);
    
    0x31 0x20 0xc3 0xa4 0x20 0xc3 0xb6 0x20 0xc3 0xbc 0x20 0x20 0xc3 0x84
    0x20 0xc3 0x96 0x20 0xc3 0x9c 0x20 0x20 0xc3 0xa9 0x20 0xc3 0x89 0x20
    0x20 0xc3 0x9f 0x20 0xe2 0x82 0xac 0x20 0x32
    

提交回复
热议问题