use printf to print character string in hex format, distorted results

前端 未结 2 1265
旧时难觅i
旧时难觅i 2021-02-06 05:00

I want to print character string in hex format,

on machine A , something like

ori_mesg = gen_rdm_bytestream (1400,seed)
sendto(machine B, ori_mesg, len(m         


        
相关标签:
2条回答
  • 2021-02-06 05:26

    Here's a small program that illustrates the problem I think you might be having:

    #include <stdio.h>
    int main(void) {
        char arr[] = { 0, 16, 127, 128, 255 };
        for (int i = 0; i < sizeof arr; i ++) {
            printf(" %2x", arr[i]);
        }
        putchar('\n');
        return 0;
    }
    

    On my system (on which plain char is signed), I get this output:

      0 10 7f ffffff80 ffffffff
    

    The value 255, when stored in a (signed) char, is stored as -1. In the printf call, it's promoted to (signed) int -- but the "%2x" format tells printf to treat it as an unsigned int, so it displays fffffffff.

    Make sure that your mesg and mesg_check arrays are defined as arrays of unsigned char, not plain char.

    UPDATE: Rereading this answer more than a year later, I realize it's not quite correct. Here's a program that works correctly on my system, and will almost certainly work on any reasonable system:

    #include <stdio.h>
    int main(void) {
        unsigned char arr[] = { 0, 16, 127, 128, 255 };
        for (int i = 0; i < sizeof arr; i ++) {
            printf(" %02x", arr[i]);
        }
        putchar('\n');
        return 0;
    }
    

    The output is:

     00 10 7f 80 ff
    

    An argument of type unsigned char is promoted to (signed) int (assuming that int can hold all values of type unsigned char, i.e., INT_MAX >= UCHAR_MAX, which is the case on practically all systems). So the argument arr[i] is promoted to int, while the " %02x" format requires an argument of type unsigned int.

    The C standard strongly implies, but doesn't quite state directly, that arguments of corresponding signed and unsigned types are interchangeable as long as they're within the range of both types -- which is the case here.

    To be completely correct, you need to ensure that the argument is actually of type unsigned int:

    printf("%02x", (unsigned)arr[i]);
    
    0 讨论(0)
  • 2021-02-06 05:30

    Yup, always print string in hex format as:

    for(i=0;till string length;i++)
    printf("%02X",(unsigned char)str[i]);
    

    you will get error when you try to print

    the whole string in one go and when printing Hex string character by character which using 'Unsigned char' if the string is in format other than 'Unsigned char'

    0 讨论(0)
提交回复
热议问题