简体   繁体   English

获取C中float类型变量的整数表示值

[英]get the integer representation value of a float type variable in C

I have the number 20 (0x14) stored in a 32-bit register. 我将数字20(0x14)存储在32位寄存器中。 The register is allocated to a C float variable representing the value 2.8e-44. 该寄存器分配给一个代表值2.8e-44的C浮点变量。 Now I want to get the hexadecimal representation of the float variable back and stored it into an integer variable in order to restore the original meaning of the information. 现在,我想取回float变量的十六进制表示形式并将其存储到整数变量中,以恢复信息的原始含义。 Is there a better way to do that, apart from doing some pointer business? 除了做一些指针业务以外,还有更好的方法吗? Here is the code: 这是代码:

#include <stdio.h>

int main()
{
    float f = 2.802597e-44;
    int nv,*pnv;
    printf("f=%f\n",f);
    printf("n=%d\n",f);
    nv=(int)f;
    printf("n=%d\n",nv);
    pnv=(int*)&f;
    printf("n=%d\n",(*pnv));
    return 0;
}

I get what I want using the pnv integer pointer. 我得到我想要的使用pnv整数指针。 Is there a better way to do that avoiding pointers and working in C? 有没有更好的方法来避免指针并在C中工作?

You can achieve your need with a union: 您可以通过工会满足您的需求:

#include <stdio.h>

int main()
{
  union { int i; float f; } fi;
  fi.f = 2.802597e-44;
  printf("f=%e\n",fi.f);
  printf("n=%d\n",fi.i);
  return 0;
}

Note that the behaviour of (int*)&f is undefined as the pointer types are unrelated. 请注意, (int*)&f的行为未定义,因为指针类型无关。 So don't approach the problem in that way. 因此,不要以这种方式解决问题。

Having checked that sizeof(float) is the same as sizeof(int) , you could do this in one of two ways: 检查sizeof(float)sizeof(int)相同之后,您可以通过以下两种方式之一进行操作:

1) Type pruning through a union consisting of a float , and an int . 1)通过由floatint组成的union来进行修剪。 Set the union using one member, and read it back with the other. 使用一个成员设置并union ,然后与另一个成员一起读回。

2) memcpy the contents of a variable of one type to the location of the variable of the other type. 2) memcpy一种类型的一个变量的内容向其他类型的可变的位置。

Of these I prefer (2): (1) might be undefined with older C standards, and (2) also works well with C++. 在这些示例中,我更喜欢(2):(1)在较旧的C标准中可能未定义,并且(2)在C ++中也能很好地工作。

You can directly cast it to integer as; 您可以将其直接转换为:

float a = 7.4;
int b = a; // this will be rounded to 7 and you will lose information

Or you can use some built-int functions like round, ceil, floor etc. 或者,您可以使用一些内置的函数,例如round,ceil,floor等。

For reference: http://www.cplusplus.com/reference/cmath/round/?kw=round 供参考: http : //www.cplusplus.com/reference/cmath/round/?kw=round

you can use type casting .. 您可以使用类型转换..

float x =3.4;
int y = (int)x;

What are you doing there is Undefined Behavior , didn't you check the warning? 您在执行的操作中存在未定义行为 ,您没有检查警告吗?

warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘double’ [-Wformat=]
     printf("n=%d\n",f);
     ^

Read this please: How do the digits 1101004800 correspond with the number 20? 请阅读以下内容: 数字1101004800与数字20如何对应?

C is considered a weakley typed langauge, which may allow to assign values that belong to different types than the variable they are being assigned with, therefore you can simply do this: C被视为弱类型的语言,它可以允许分配与为其分配的变量不同类型的值,因此您可以简单地执行以下操作:

 int integer = 1;  
 float floater =1.1111;

 floater = integer;  

This is known as Implicit type conversion, also known as coercion, is an automatic type conversion by the compiler. 这称为隐式类型转换,也称为强制转换,是编译器自动进行的类型转换。 Some programming languages allow compilers to provide coercion; 某些编程语言允许编译器提供强制性。 others require it. 其他人需要它。

but consider the following: 但请考虑以下几点:

  • what happens when the float is less than zero? 浮点数小于零时会发生什么?

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

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