[英]cast the memory stored in a uint32_t as a float in C
将十六进制表示为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表示形式强制转换为float(不是必需的操作),我不知道如何告诉内存它实际上是一个浮点数
仅供以后参考,如@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
持有真实的浮点数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.