我有一个char*数组,定义为char *data_ptr = "ABCDEFGHIJKLMNOPQRSTUVWX"
。另外,我有一个声明为static uint32_t wr_buff[32]
的写缓冲区。
我迭代地从数组中提取4个字节,并将它们放入定义为char substr[4]
的char子字符串中。
我想要做的是将这4个字节放入写缓冲区,而不改变它们的值,但是我首先需要将子字符串的每个元素转换为uint32_t。
我试过以下几句话,但没有奏效:
wr_buffer = (uint32_t)substr[0] << 24 | (uint32_t)substr[1] << 16 | (uint32_t)substr[2] << 8 | (uint32_t)substr[3];
如何将这4个字节保存到uint32_t缓冲区中?
发布于 2022-03-11 11:33:15
你为了复杂的事情:
char *data_ptr = "ABCDEFGHIJKLMNOPQRSTUVWX";
static uint32_t wr_buff[32];
char *posInEntry ;
char *posInOutput;
posInEntry = data_ptr;
posInOutput = (char *) wr_buff;
while (posInEntry < strlen(data_ptr)) {
posInOutput[3] = posInEntry++;
posInOutput[2] = posInEntry++;
posInOutput[1] = posInEntry++;
posInOutput[0] = posInEntry++;
posInOutput += 4;
}
注意(感谢@ user16217248指出了几个错误):
在stdint.h
中定义了
https://stackoverflow.com/questions/71436389
复制相似问题