在DWORD中设置特定的字节
Set specific byte in DWORD
如何在4字节长度的DWORD变量中设置特定的字节?
DWORD color_argb;
unsigned char a = 11; // first byte
unsigned char r = 22; // second byte
unsigned char g = 33; // third byte
unsigned char b = 44; // fouth byte
zumalifeguard,如果我理解正确的话-我可以使用next宏:
#define SET_COLOR_A(color, a) color |= (a << 24)
#define SET_COLOR_R(color, r) color |= (r << 16)
#define SET_COLOR_G(color, g) color |= (g << 8)
#define SET_COLOR_B(color, b) color |= (b << 0)
?
试试这些宏:
#define SET_COLOR_A(color, a) color = (DWORD(color) & 0x00FFFFFF) | ((DWORD(a) & 0xFF) << 24)
#define SET_COLOR_R(color, r) color = (DWORD(color) & 0xFF00FFFF) | ((DWORD(r) & 0xFF) << 16)
#define SET_COLOR_G(color, g) color = (DWORD(color) & 0xFFFF00FF) | ((DWORD(g) & 0xFF) << 8)
#define SET_COLOR_B(color, b) color = (DWORD(color) & 0xFFFFFF00) | (DWORD(b) & 0xFF)
重要的是保留未被操作的现有位,而删除被替换的现有位。
OR
是不够的。DWORD color_argb;
unsigned char a = 11; // first byte
unsigned char r = 22; // second byte
unsigned char g = 33; // third byte
unsigned char b = 44; // fouth byte
color_argb = 0;
int byte_number; // first byte = 1, second byte = 2, etc.
// Set first byte to a;
byte_number = 1;
color_argb |= ( a << (8 * (4 - byte_number) ) );
// Set first byte to a;
byte_number = 2;
color_argb |= ( b << (8 * (4 - byte_number) ) );
// Set first byte to a;
byte_number = 3;
color_argb |= ( c << (8 * (4 - byte_number) ) );
// Set first byte to a;
byte_number = 4;
color_argb |= ( d << (8 * (4 - byte_number) ) );
相关文章:
- std::ifstream::read 不会读取所有 512 字节,并设置 EOF 和失败位
- 复制后删除原始数组指针将前 3 个字节设置为 0
- 是否强制转换void**并将第一个字节设置为nullptr
- 蓝牙LE:设置字节数组的特征会发送错误的值
- 如果字节中的所有位都设置为高,则设置为高字节
- 在 C++ 中立即设置故障位(在读取任何字节之前)
- 如何在没有循环的字节中设置位
- 你能为 Asio 的read_until设置字节限制吗?
- 以 C++ 为单位设置(短整型)的上下字节
- 如何在C++中设置短整型的上限和下限字节
- 如何正确设置fwrite()的大小和计数参数以编写一块字节
- 函数返回布尔值,只设置整个寄存器的1个字节
- 将 int 的所有字节设置为(无符号字符)0,保证表示零
- 如何在 Windows 中将文件大小设置为 100 字节 C++.
- C++/编译:是否可以设置VPTR(全局vtable + 2字节索引)的大小
- 如何使用 #define 语句来设置字节数组
- 如何告诉GCC通过编译器设置(不是编译指示)将结构大小边界设置为4字节?
- 设置 for 循环以循环,直到检测到空字节
- Java字节设置操作中的字节出现符号(+/-)错误
- 正在将char数组的最后8个字节设置为__uint64