Converting an int to a 2 byte hex value in C

后端 未结 15 1427
不知归路
不知归路 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 19:05

    you can use this it is simple and easy to convert

    typedef union {
        struct hex{
                unsigned  char a;
                unsigned  char b;
    
        }hex_da;
        short int dec_val;
    }hex2dec;
    
    hex2dec value;
    value.dec_val=10;
    

    Now hex value is stored in hex_da structure access it for further use.

提交回复
热议问题