如何将十六进制字符串转换为浮点值 [英] How Do I Convert Hex String To Float Value

查看:144
本文介绍了如何将十六进制字符串转换为浮点值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个十六进制字符串="41BD9A69",我需要将此十六进制字符串转换为ASCII十六进制IEEE Float.
我的意思是我们如何以编程方式在C ++中或使用库将此十六进制字符串转换为等效的float值23.700396.
十六进制字符串="41BD9A69" = 23.700396

I have a Hex String = "41BD9A69", I need to convert this hex string into ASCII Hex IEEE Float.
I meant how do we convert this hex string into equivalent float value of 23.700396 programatically in C++ or using library.
Hex String = "41BD9A69" = 23.700396

推荐答案

您可以使用联合技巧:
You might use the union trick:
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

union ulf
{
    unsigned long ul;
    float f;
};

int main()
{
    ulf u;
    string str = "41BD9A69";
    stringstream ss(str);
    ss >> hex >> u.ul;
    float f = u.f;
    cout << f << endl;
}


它并不复杂,但请看这里: http ://gregstoll.dyndns.org/~gregstoll/floattohex/ [ ^ ]-它是一个计算器模块,其中包括C源代码即可做到这一点.
It''s not complex, but look here: http://gregstoll.dyndns.org/~gregstoll/floattohex/[^] - it''s a calculator module, that includes C source to do just that.


这应该可以解决您的问题

This should solve your problem

float f;
long l;

l = strtol(HEXString, (char**)NULL, 16);
f = (float)l;

printf("%f", f);



请参阅下面的详细说明

在c中将十六进制转换为浮点数,反之亦然 [ ^ ]



See below for detail explanation

Convert a hexadecimal to a float and viceversa in c[^]


这篇关于如何将十六进制字符串转换为浮点值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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