如何在Visual C ++ 2010中将十六进制(浮点数)转换为十进制数

时间:2013-03-27 07:50:46

标签: visual-studio-2010 visual-c++

我正在Visual c + + + Win Forms Application中开发一个应用程序,我将收到十六进制数据(数据包)并将其存储在Rich Text Box中。收到的十六进制数据将表示浮点数。对于Ex:浮点数11.62将作为4139eb85(存储在RichTextBox中的字符串)接收。我想将十六进制数转换为原始值并显示它。我正努力将4139EB85转换为原始值11.62

Pl最早帮助我。

2 个答案:

答案 0 :(得分:2)

不便携,但是:

int x = 0x4139eb85;
float y = *reinterpret_cast<float *>(&x);

答案 1 :(得分:1)

您可以将字符串解析为UInt32,然后使用BitConverter::ToSingle将字节转换为浮点数:

// Convert the hex string into a UInt32 (if necessary)
UInt32 bits = UInt32::Parse("4139EB85", System::Globalization::NumberStyles::HexNumber);
// Convert the bytes of the UInt32 to a Single/float
float f = BitConverter::ToSingle(BitConverter::GetBytes(bits), 0);