Converting number to byte array

前端 未结 3 640
别跟我提以往
别跟我提以往 2021-01-06 16:50

Hi I have a base 10 number for example 3198, and the hex representation is 0x0C7E

How do I convert that number to hex and put that hex value in a byte array in the f

3条回答
  •  梦毁少年i
    2021-01-06 17:56

    #include 
    
    union uint32_value {
        unsigned int value;
    
        struct little_endian {
            unsigned char fou;
            unsigned char thi;
            unsigned char sec;
            unsigned char fir;
        } le;
    
        struct big_endian {
            unsigned char fir;
            unsigned char sec;
            unsigned char thi;
            unsigned char fou;
        } be;
    };
    
    int main(void)
    {
        union uint32_value foo;
        foo.value = 3198;
        printf("%02x %02x %02x %02x\n", foo.le.fir, foo.le.sec, foo.le.thi, foo.le.fou);
    
        return 0;
    }
    

提交回复
热议问题