Converting an int to a 2 byte hex value in C

后端 未结 15 1415
不知归路
不知归路 2020-12-05 18:17

I need to convert an int to a 2 byte hex value to store in a char array, in C. How can I do this?

15条回答
  •  天命终不由人
    2020-12-05 18:55

    If you're allowed to use library functions:

    int x = SOME_INTEGER;
    char res[5]; /* two bytes of hex = 4 characters, plus NULL terminator */
    
    if (x <= 0xFFFF)
    {
        sprintf(&res[0], "%04x", x);
    }
    

    Your integer may contain more than four hex digits worth of data, hence the check first.

    If you're not allowed to use library functions, divide it down into nybbles manually:

    #define TO_HEX(i) (i <= 9 ? '0' + i : 'A' - 10 + i)
    
    int x = SOME_INTEGER;
    char res[5];
    
    if (x <= 0xFFFF)
    {
        res[0] = TO_HEX(((x & 0xF000) >> 12));   
        res[1] = TO_HEX(((x & 0x0F00) >> 8));
        res[2] = TO_HEX(((x & 0x00F0) >> 4));
        res[3] = TO_HEX((x & 0x000F));
        res[4] = '\0';
    }
    

提交回复
热议问题