分配一个十六进制值以浮点数返回错误的值 [英] assign a hex value to float returns a wrong value

查看:31
本文介绍了分配一个十六进制值以浮点数返回错误的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C语言的新手,我尝试了以下操作:

I'm new to C and I tried following:

float f = 0x41933333;

这应该给我浮点数: 18.4 ,但是当我查看内存时,我发现了十六进制值:

this should give me the float number: 18.4 but when I look into the memory I find the hex value:

66 26 83 4e

其为浮点数: 41933333.0000

为什么我的十六进制值解释为十进制数字?

Why is my hex value interpreted as a decimal number?

推荐答案

或者,您可以通过将int的值解释为float来执行强制转换

alternatively, you can perform a casting by interpreting the value of an int as float

unsigned int n = 0x41933333;
float f = *((float*)&n);

这篇关于分配一个十六进制值以浮点数返回错误的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆