使用字符数组作为长整数数组

Nic*_*k T 1 c arrays avr avr-gcc unions

在我的AVR上,我有一个字符数组,它以{R,G,B,x,R,G,B,x,...}的形式保存颜色强度信息(x是未使用的字节).有没有简单的方法来写一个long int(32位),char myArray[4*LIGHTS]所以我可以轻松写一个0x00BBGGRR数字?

我的类型转换是粗糙的,我不知道如何写它.我猜测只是制作一个指向long int类型的指针并将其设置为等于myArray,但后来我不知道如何随意告诉它将group x设置为myColor.

uint8_t myLights[4*LIGHTS];
uint32_t *myRGBGroups = myLights; // ?

*myRGBGroups = WHITE; // sets the first 4 bytes to WHITE
                      // ...but how to set the 10th group?
Run Code Online (Sandbox Code Playgroud)

编辑:我不确定类型转换是否是正确的术语,因为我认为如果它只是将32位数字截断为8位?

Ste*_*son 5

typedef union {
    struct {
         uint8_t    red;
         uint8_t    green;
         uint8_t    blue;
         uint8_t    alpha;
    }          rgba;
    uint32_t   single;
} Color;

Color    colors[LIGHTS];

colors[0].single = WHITE;
colors[0].rgba.red -= 5;
Run Code Online (Sandbox Code Playgroud)

注意:在little-endian系统中,4字节值的低位字节将是alpha值; 而它将是大端系统的红色值.