在c中具有以字符串表示的十六进制
例如char* text = "0xffff"
我设法通过以下功能将数据保存在uint32_t
中:
for (unsigned int i = 0; i < line_length && count < WORD_SIZE; i++) {
char c[2]; //represent the digit as string
c[0] = line[i];
c[1] = '\0';
if (isxdigit(c[0])) { //we've found a relevant char.
res_out <<= 4; // shift left by 4 for the next 4 bits.
res_out += (int32_t)strtol(c, NULL, 16); //set the last 4 bits bit to relevant value
//res_out <<= 4; // shift left by 4 for the next 4 bits.
count += 4;
}
}
现在,具有32位的uint32_t 有时表示一个单精度浮点数,我想这样解析它
当然使用float f = (float)num
会将int表示形式强制转换为浮点数(而不是所需的操作),我不知道如何告诉内存它实际上是浮点数
答案 0 :(得分:0)
仅供参考,如@melpomene建议
uint32_t x = /* some single precision float value dumped into a uint32_t*/;
uint32_t float_placeholder = 0;
memcpy(&float_placeholder, &x, sizeof(uint32_t));
float_placeholder
包含真实的浮点数