[英]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.