variables – How to convert integer value to unsigned char uint8_t in C

From your unclear question, I’m guessing you want to use a union:

#include <stdio.h>
#include <stdint.h>

int main(void) {
    union {
        int x;
        uint8_t device[ sizeof(int) ];
    } foo;
    
    foo.x = 1234; // Set the Integer Field
    
    for( int i=0; i < sizeof(int); ++i)
    {
        printf("Byte %d is : 0x%02X\n", i, foo.device[i] );
        // Print the Byte-Array Field
    }
    
    return 0;
}

Variable foo can be interpreted as a 4-byte integer (by field x) and also as a 4-byte array of unsigned char values (by field device)

Output:

Byte 0 is : 0xD2
Byte 1 is : 0x04
Byte 2 is : 0x00
Byte 3 is : 0x00

Because integer 0x04D2(hex) is 1234(decimal)

Read more here: Source link