Converting an int to a 2 byte hex value in C

后端 未结 15 1420
不知归路
不知归路 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:59

    unsigned int hex16 = ((unsigned int) input_int) & 0xFFFF;
    

    input_int is the number you want to convert. hex16 will have the least significant 2 bytes of input_int. If you want to print it to the console, use %x as the format specifier instead of %d and it will be printed in hex format.

提交回复
热议问题