[英]cast the memory stored in a uint32_t as a float in C
Having a Hex represented as a string in c 将十六进制表示为c中的字符串
eg char* text = "0xffff"
例如,
char* text = "0xffff"
I manage to hold the data in a uint32_t
with the following function: 我设法通过以下功能将数据保存在
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;
}
}
Now, having the 32 bits, the uint32_t sometimes represented a single-precision floating point number, and I would like to parse it as such 现在,具有32位,uint32_t 有时表示一个单精度浮点数,我想这样解析它
Using float f = (float)num
of course casts the int representation to float (not the needed operation) and I have no other idea's how to tell memory it's actually a floating point number 当然使用
float f = (float)num
会将int表示形式强制转换为float(不是必需的操作),我不知道如何告诉内存它实际上是一个浮点数
Just for future references, As @melpomene suggested 仅供以后参考,如@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
holds the true floating point number float_placeholder
持有真实的浮点数
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.