[英]How to cast char* to uint32_t in C?
我有一个 char* 数组,定义为char *data_ptr = "ABCDEFGHIJKLMNOPQRSTUVWX"
。 另外,我有一个写缓冲区声明为static uint32_t wr_buff[32]
。
我迭代地从数组中提取 4 个字节,并将它们放入定义为char substr[4]
的 char 的 substring 中。
我想要做的是将这 4 个字节放入写入缓冲区而不更改它们的值,但我首先需要将 substring 的每个元素转换为 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 缓冲区?
你过于复杂的事情:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.