我正在尝试这样做:
typedef struct {
uint16_t red : 6;
uint16_t green : 5;
uint16_t blue : 5;
} color_t
然后我想得到类似的东西:
color_t clr;
clr.red = 0;
clr.green = 10;
clr.blue = 15;
并将复合变量clr写入int:
int value = clr; // this does not work
fprintf(draw, "%4X", value);
我这样做的原因是,我想创建橙色,紫色等颜色,并从文件中将其绘制在屏幕上。在文件中,我正在以十六进制格式编写颜色。
另一件事是,我想稍后在我的代码中这样做:
if (clr == value) { ... }
或者换句话说,我想比较struct位域中的值和int保持颜色的真正十六进制值。
这是一个独立的示例,显示了如何使用联合访问具有位字段的结构的值:
#include <stdio.h>
#include <stdlib.h>
typedef struct {
uint16_t red: 6;
uint16_t green: 5;
uint16_t blue: 5;
} color_t;
typedef union {
uint16_t color_value;
color_t color_bits;
} color_helper_t;
int main(void) {
color_helper_t clr;
clr.color_bits.red = 0;
clr.color_bits.green = 0;
clr.color_bits.blue = 15;
uint16_t value = clr.color_value;
printf("%04X\n", value);
if (clr.color_value == value) {
printf("The values are equal\n");
}
return 0;
}
输出
7800
The values are equal
我认为您最好的选择是编写一个像这样的简单转换器函数:
uint16_t colorToInt(color_t c)
{
uint16_t ret = 0;
ret |= c.blue;
ret |= c.green << 5;
ret |= c.red << 10;
return ret;
}