简体   繁体   English

在C中将大整数转换为浮点数

[英]Converting a large integer to a floating point number in C

I recently wrote a block of code that takes as an input an 8 digit hexadecimal number from the user, transforms it into an integer and then converts it into a float. 我最近编写了一个代码块,用来输入用户的8位十六进制数,将其转换为整数,然后将其转换为浮点数。 To go from integer to float I use the following: 要从整数到浮点数,我使用以下内容:

int myInt;          
float myFloat;
myFloat = *(float *)&myInt;
printf("%g", myFloat);

It works perfectly for small numbers. 它适用于小数字。 But when the user inputs hexadecimal numbers such as: 但是当用户输入十六进制数字时,例如:

0x0000ffff
0x7eeeeeef

I get that myInt = -2147483648 and that myFloat = -0. 我得到myInt = -2147483648,myFloat = -0。 I know that the number I get for myInt is the smallest possible number that can be stored in an int variable in C. 我知道我为myInt获得的数字是可以存储在C中的int变量中的最小可能数字。

Because of this problem, the input range of my program is extremely limited. 由于这个问题,我的程序的输入范围非常有限。 Does anyone have any advice on how I could expand the range of my code so that it could handle a number as big as: 有没有人对如何扩展我的代码范围有任何建议,以便它可以处理一个大到的数字:

0xffffffff

Thank you so much for any help you may give me! 非常感谢你给我的任何帮助!

The correct way to get the value transferred as accurately as float will allow is: 获得与float一样准确传递的值的正确方法是:

float myFloat = myInt;

If you want better accuracy, use double instead of float . 如果您想要更好的准确性,请使用double而不是float

What you're doing is trying to reinterpret the bit pattern for the int as if it was a float , which is not a good idea. 你正在做的是试图重新解释int的位模式,好像它是一个float ,这不是一个好主意。 There are hexadecimal floating-point constants and conversions available in C99 and later. C99及更高版本中提供了十六进制浮点常数和转换。 (However, if that's what you are trying, your code in the question is correct — your problem appears to be in converting hex to integer.) (但是,如果这是您正在尝试的,那么问题中的代码是正确的 - 您的问题似乎是将十六进制转换为整数。)

If you get -2147483648 from 0x0000FFFF (or from 0x7EEEFFFF), there is a bug in your conversion code. 如果从0x0000FFFF(或从0x7EEEFFFF)获得-2147483648 ,则转换代码中存在错误。 Fix that before doing anything else. 在做任何其他事情之前修复它。 How are you doing the hex to integer conversion? 你是如何进行十六进制到整数转换的? Using strtol() is probably a good way (and sscanf() and friends is also possible), but be careful about overflows.) 使用strtol()可能是一个好方法(并且sscanf()和朋友也是可能的),但要注意溢出。)

Does anyone have any advice on how I could expand the range of my code so that it could handle a number as big as 0xffffffff 有没有人对如何扩展我的代码范围有任何建议,以便它可以处理像0xffffffff一样大的数字

You can't store 0xffffffff in a 32-bit int ; 你不能将0xffffffff存储在32位int ; the largest positive hex value you can store in a 32 bit int is 0x7FFFFFFF or (2^31 -1) or 2147483647, but the negative range is -2^31 or -2147483648, 您可以存储在32位int的最大正十六进制值是0x7FFFFFFF或(2 ^ 31 -1)或2147483647,但负范围是-2 ^ 31或-2147483648,

The ranges are due to obvious limitations in the number of bits available and the 2's complement system. 范围是由于可用位数和2的补码系统的明显限制。

Use an unsigned int if you want 0xffffffff. 如果你想要0xffffffff,请使用unsigned int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM