IEEE float十六进制424ce027要浮动吗? [英] IEEE float hex 424ce027 to float?

查看:75
本文介绍了IEEE float十六进制424ce027要浮动吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个IEEE浮点十六进制424ce027,如何将其转换为十进制?

If I have a IEEE float hex 424ce027, how do I convert it to decimal?

unsigned char ptr[] = {0x42,0x4c,0xe0,0x27};

怎么办?

float tmp = 51.218899;

推荐答案

也许...

float f = *reinterpret_cast<float*>(ptr);

尽管在我的x86机器上,我还必须反转字符的字节顺序以获得所需的值.

Although on my x86 machine here I had to also reverse the byte order of the character to get the value you wanted.

std::reverse(ptr, ptr + 4);
float f = *reinterpret_cast<float*>(ptr);

您可能想要使用sizeof(float)而不是4或其他方式来获取大小.您可能需要反转字节的副本,而不是原始的.但是您这样做却有些丑陋.

You might want to use sizeof(float) instead of 4 or some other way to get the size. You might want to reverse a copy of the bytes, not the original. It's somewhat ugly however you do it.

编辑:正如注释中指出的那样,此代码不安全,因为它创建了两个别名相同的内存但类型不同的指针.它可以在特定的编译器上正常工作.程序,但不受该标准的保证.

edit: As pointed out in the comments, this code is unsafe as it creates two pointers aliasing the same memory but of different types. It may well work on a specific compiler & program, but isn't guarenteed to by the standard.

这篇关于IEEE float十六进制424ce027要浮动吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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