十六进制到浮点转换 [英] hexadecimal to float conversion

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

问题描述



我想将单精度十六进制数转换为

浮点数。以下程序似乎工作正常..

但我不想使用scanf。我已经有一个32位的十六进制

数,并希望将其转换为float。谁能告诉我怎么用

呢?


int main()

{

float theFloat;

{

scanf("%f",& theFloat);

printf(" 0x%08X,% f \ n",*(int *)& theFloat,theFloat);

}

返回0;

}

Hi,
I would like to convert a single precision hexadecimal number to
floating point. The following program seems to work fine..
But I do not want to use scanf. I already have a 32 bit hexadecimal
number and would like to convert it into float. Can anyone tell me how
to do it?

int main()
{
float theFloat;
{
scanf("%f", &theFloat);
printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);
}
return 0;
}

推荐答案

2005年8月18日18:59:11 -0700, pa ************** @ gmail.com

comp.lang.c中写道:
On 18 Aug 2005 18:59:11 -0700, pa**************@gmail.com wrote in
comp.lang.c:

我想将单精度十六进制数转换为
浮点数。以下程序似乎工作正常..
但我不想使用scanf。我已经有一个32位十六进制的数字,并希望将其转换为float。谁能告诉我怎么做呢?


您的帖子非常不清楚,您的示例代码似乎与
相匹配。什么是单精度十六进制数?如果你使用scanf()
,你显然有一个文本字符串。这个

文本字符串是如何创建的?


#include< stdio.h> / *需要scanf()的原型* /

#include< stdlib.h> / *需要strto ...()* /

int main()
{浮动浮动;
{
scanf("%) f",& theFloat);


我不明白,你好像已经在这里有一个漂浮物。它显示你的scanf()实现理解并以某种十六进制格式解析

浮点值。最不寻常的。

printf(" 0x%08X,%f \ n",*(int *)& theFloat,theFloat);
}
返回0;
}
Hi,
I would like to convert a single precision hexadecimal number to
floating point. The following program seems to work fine..
But I do not want to use scanf. I already have a 32 bit hexadecimal
number and would like to convert it into float. Can anyone tell me how
to do it?
Your post is extremely unclear, and your sample code does not seem to
match it. What is a "single precision hexadecimal number"? If you
are using scanf() you apparently have a text string. How was this
text string created?

#include <stdio.h> /* needed for prototype of scanf() */
#include <stdlib.h> /* needed for strto...() */
int main()
{
float theFloat;
{
scanf("%f", &theFloat);
I don''t understand, you seem to have a float right here already. It
appears that your implementation of scanf() understands and parses
floating point values in some hex format. Most unusual.
printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);
}
return 0;
}




真正安全的替代方法是使用fgets()将字符串读入

缓冲区并使用strtod( )将其解析为double。如果scanf()是

给你正确的值,我猜strtod()也会。但是我觉得
很难相信scanf()正在将一串六角形

变成一个浮点数。


也许您键入的代码实际上并不是您真正的代码所做的事情?


-

Jack Klein

主页: http://JK-Technology.Com

常见问题

comp.lang.c http:// www.eskimo.com/~scs/C-faq/top.html

comp.lang.c ++ http://www.parashift.com/c++-faq-lite/

alt.comp.lang。 learn.c-c ++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html



The truly safe alternative is to use fgets() to read the string into a
buffer and use strtod() to parse it into a double. If scanf() is
giving you the correct value, I guess strtod() will as well. But I
have a hard time believing that scanf() is turning a string of hex
into a float.

Perhaps the code you typed is not actually what your real code does?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html


pa ************** @ gmail.com 写道:
我想将单精度十六进制数转换为
浮点数。以下程序似乎工作正常..
但我不想使用scanf。我已经有一个32位十六进制的数字,并希望将其转换为float。谁能告诉我怎么做呢?

int main()
{漂浮浮子;
{
scanf(" %f"& theFloat);
printf(" 0x%08X,%f\ n",*(int *)& theFloat,theFloat);
}
返回0;
}
I would like to convert a single precision hexadecimal number to
floating point. The following program seems to work fine..
But I do not want to use scanf. I already have a 32 bit hexadecimal
number and would like to convert it into float. Can anyone tell me how
to do it?

int main()
{
float theFloat;
{
scanf("%f", &theFloat);
printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);
}
return 0;
}




该代码(如果添加必要的#include指令)从中读取

浮点值stdin并打印一个十六进制的字符串。

这与你要求的相反,所以说它似乎

工作得很好。是奇怪的。


你的代码假设int和float具有相同的大小(并且可能

具有相同的对齐要求)。这不是一个安全的假设。


您需要准确定义单精度

十六进制数的含义。我认为*你的意思是一个十六进制字符串

对应于浮点值的表示。如果

那么,这也是不便携的。


例如,这个程序:


#include< stdio.h>


int main(无效)

{

float f = 42.5;

unsigned int u = *(unsigned int *)& f;

printf(" f =%g \ n",f);

printf(" u = 0x%X \ n,u);

返回0;

}


碰巧打印


f = 42.5

u = 0x422A0000


在我刚试过的一个实现上,但是十六进制

数字在不同的系统上可能会有所不同。这些

天的大多数系统使用IEEE浮点格式,但字节顺序可能不同。


-

Keith Thompson( The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



That code (if you add the necessary #include directive) reads a
floating-point value from stdin and prints a hexadecimal string.
That''s the opposite of what you''re asking for, so saying it "seems to
work fine" is odd.

Your code assumes that int and float have the same size (and probably
the same alignment requirements). This is not a safe assumption.

You need to define exactly what you mean by "a single precision
hexadecimal number". I *think* you mean a hexadecimal string
corresponding to the representation of the floating-point value. If
so, this is also going to be non-portable.

For example, this program:

#include <stdio.h>

int main(void)
{
float f = 42.5;
unsigned int u = *(unsigned int*)&f;
printf("f = %g\n", f);
printf("u = 0x%X\n", u);
return 0;
}

happens to print

f = 42.5
u = 0x422A0000

on one implementation where I just tried it, but the hexadecimal
digits could be different on different systems. Most systems these
days use IEEE floating-point formats, but byte order can vary.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


pa**************@gmail.com 写道:

我想将单精度十六进制数转换为
浮点。以下程序似乎工作正常..
但我不想使用scanf。我已经有一个32位十六进制的数字,并希望将其转换为float。谁能告诉我怎么做呢?

int main()
{漂浮浮子;
{
scanf(" %f",& theFloat);


这会以通常的形式读取一组字符,用于

a浮点常量,将它们转换为浮动值,

并将结果存储在`theFloat''中。或者它可能会失败

,因为没有更多的输入,或者因为输入流

提供了蓝色夏威夷。而不是看起来像某个数字或某些其他原因的东西。你应该检查一下scanf()函数返回的

值,看它是否实际转换了
并存储了一个数字。

printf( 0x%08X,%f \ n,*(int *)& theFloat,theFloat);
Hi,
I would like to convert a single precision hexadecimal number to
floating point. The following program seems to work fine..
But I do not want to use scanf. I already have a 32 bit hexadecimal
number and would like to convert it into float. Can anyone tell me how
to do it?

int main()
{
float theFloat;
{
scanf("%f", &theFloat);
This reads a group of characters in the usual form for
a floating-point constant, converts them to a `float'' value,
and stores the result in `theFloat''. Or perhaps it fails
because there''s no more input, or because the input stream
provided "blue Hawaii" instead of something that looks like
a number, or for some other reason. You should check the
value returned by the scanf() function to see whether it has
actually converted and stored a number.
printf("0x%08X, %f\n", *(int *)&theFloat, theFloat);




输出可能有意义,也可能没有意义;在某些机器上

它可能根本不起作用(例如,你可能会得到SIGBUS

或其他类型的陷阱)。你试图假装

表示浮点值'theFloat''可以

也被理解为'int'',这可能会或者可能会不是这样。

许多机器都会给出某种结果,但不同的机器可能会产生不同的结果,而C语言则是b $


C确实为受限制的表格提供一个特殊保证

类型 - 惩罚:允许通过类型指针

` unsigned char *''访问任何对象表示的单个字节

。例如,你可以做


unsigned char * p =(unsigned char *)& theFloat;

printf(" 0x");

while(p<(unsigned char *)(& theFloat + 1))

printf("%02X",* p ++);

printf(",%f\ n",theFloat);


一次打印一个字节。 (顺便说一下,这并不是完美的,因为一个字节可以有超过8位而且因此超过两个十六进制数字。)这是

这种情况​​很少见,但确实存在。)这个循环保证

产生输出而不是陷阱或类似的东西,但
$ b $你从机器A获得的输出可能与机器B的输出不匹配。


然而,你实际上问过另一种方式:来自

a十六进制流数字到浮点值。你可以使用一个循环来一次转换一个字节(上面显示的是反向的b $ b),但生成42.0的字节流机器A上的
可能在机器B上产生-273.16,或者在NaN上产生-273.16,或者即使是在尝试使用它时陷阱的信号NaN也会产生
。有一个
是没有一个真实的浮点值表示为

十六进制字符串 - 所以,你究竟想要做什么?
< br $>
-

Eric Sosman
es *** **@acm-dot-org.inva 盖子



The output may or may not be meaningful; on some machines
it might not work at all (for example, you might get SIGBUS
or some other kind of trap). You are trying to pretend that
the representation of the floating-point value `theFloat'' can
also be understood as an `int'', and this may or may not be so.
Many machines will give some kind of result, but different
machines are likely to give different results, and the C language
itself doesn''t guarantee any result at all.

C does provide one special guarantee for a restricted form
of type-punning: it is permitted to access the individual bytes
of any object''s representation through a pointer of the type
`unsigned char*''. For example, you could do

unsigned char *p = (unsigned char*)&theFloat;
printf ("0x");
while (p < (unsigned char*)(&theFloat + 1))
printf ("%02X", *p++);
printf (", %f\n", theFloat);

to print the individual bytes one at a time. (Even this isn''t
perfect, by the way: a byte can have more than eight bits and
hence more than two hexadecimal digits. Machines where this is
the case are rare, but do exist.) This loop is guaranteed to
produce output and not to trap or anything like that, but the
output you get from machine A may not match that from machine B.

However, you actually asked about going the other way: from
a stream of hexadecimal digits to a floating-point value. You
could use a loop to convert the bytes one at a time (the inverse
of what''s shown above), but the byte stream that produces 42.0
on machine A might produce -273.16 on machine B, or a NaN, or
even a signalling NaN that traps when you try to use it. There
is no One True Representation of a floating-point value as a
hexadecimal string -- so what, exactly, are you trying to do?

--
Eric Sosman
es*****@acm-dot-org.invalid


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

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