IEEE 754到C语言的十进制

我正在寻找将浮点数转换为C中的十进制表示的最佳方法。我将尝试给出一个示例:用户在IEEE754(1 1111111 10101 …)中引入一个数字,程序必须返回十进制表示(例如25.6)
我尝试过使用掩码和按位操作,但我没有任何逻辑结果。

我相信以下是执行您描述的操作:

我使用int作为中间表示,因为它与float(在我的机器上)具有相同的位数,并且允许从二进制字符串轻松转换。

 #include  union { int i; float f; } myunion; int binstr2int(char *s) { int rc; for (rc = 0; '\0' != *s; s++) { if ('1' == *s) { rc = (rc * 2) + 1; } else if ('0' == *s) { rc *= 2; } } return rc; } int main(void) { // the input binary string (4 bytes) char * input = "11000000110110011001100110011010"; float *output; // convert to int, sizeof(int) == sizeof(float) == 4 int converted = binstr2int(input); // strat 1: point memory of float at the int output = (float*)&converted; // cast to suppress warning printf("%f\n", *output); // -6.8 // strat 2: use a union to share memory myunion.i = converted; printf("%f\n", myunion.f); // -6.8 return 0; } 

正如@DanielKamilKozar指出的那样,该int的正确类型是uint32_t 。 但是,这需要包括