无法将浮点值保存到位域结构

Maj*_*han 3 c c++

我有一个结构

struct {
   u32 var1 :7;
   u32 var2 :4;
   u32 var3 :4;
   u32 var4 :1;
   u32 var5 :4;
   u32 var6 :7;
   u32 var7 :4;
   u32 var8 :1;
        } my_struct;

my_struct struct1[10];

for(int i=0;i<10; i++)
  {
    // left some portion
    struct1[i].var5= x;// where x is a float value retrieved from a database with sqlapi++ asDouble()

    cout<<"Value of x from db is:\t"<<x;   // prints 0.1 if it is stored, prints 2.4 if 2.4 is fed

    cout<<"Value of x stored in struct1 is:\t"<<struct1[i].var5;   // prints 0 instead of 0.1, prints 2 instead of 2.4
  }
Run Code Online (Sandbox Code Playgroud)

我想在 var5 中存储浮点值,例如 0.1、3.4、0.8。但我无法这样做。有人可以帮我如何解决这个问题吗?

ryy*_*ker 5

您可以通过几个中间步骤来完成您所要求的操作。首先将 float 转换为 int,然后将该 int 转换为二进制表示形式。从那里,您可以将结果值分配给您的位字段。该答案仅涉及中间步骤。

这里的信息提供了5.2 float由 表示的背景和佐证01000000101001100110011001100110。将浮点数分解为二进制表示可以通过多种不同的方式完成。这只是一种实现或表示。反转此过程(即从二进制返回浮点)将需要向后遵循链接中列出的同一组规则。

注意:字节序也是一个因素,我在 Windows/Intel 环境中运行了这个。

代码:

#include <stdio.h>      /* printf */
#include <stdlib.h>     /* strtol */

const char *byte_to_binary32(long int x);
const char *byte_to_binary64(__int64 x);
int floatToInt(float a);
__int64 doubleToInt(double a);

int main(void)
{
     long lVal, newInt;
     __int64 longInt;
    int i, len, array[65];
    int len1, len2, len3, len4, len5, len6;
    char buf[100];
    char quit[]={" "};
    float fNum= 5.2;
    double dpNum= 5.2;
    long double ldFloat;
    
    while(quit[0] != 'q')
    {
        printf("\n\nEnter a float number: ");
        scanf("%f", &fNum);
        printf("Enter a double precision number: ");
        scanf("%Lf", &ldFloat);

        newInt = floatToInt(fNum);
        {
            //float
            printf("\nfloat: %6.7f\n", fNum);  
            printf("int: %d\n", newInt);  
            printf("Binary: %s\n\n", byte_to_binary32(newInt));
        }
        longInt = doubleToInt(dpNum);
        {
            //double
            printf("double: %6.16Lf\n", ldFloat);  
            printf("int: %lld\n", longInt);  
            printf("Binary: %s\n\n", byte_to_binary64(longInt));  
            /* byte to binary string */
            sprintf(buf,"%s", byte_to_binary64(longInt));
        }
        len = strlen(buf);
        for(i=0;i<len;i++)
        {   //store binary digits into an array.
            array[i] = (buf[i]-'0');    
        }
        //Now you have an array of integers, either '1' or '0'
        //you can use this to populate your bit field, but you will
        //need more fields than you currently have.

        printf("Enter any key to continue or 'q' to exit.");
        scanf("%s", quit);
    }
    return 0;
}

const char *byte_to_binary32(long x)
{
    static char b[33]; // bits plus '\0'
    b[0] = '\0';
    char *p = b;  

    unsigned __int64 z;
    for (z = 2147483648; z > 0; z >>= 1)       //2^32
    {
        *p++ = (x & z) ? '1' : '0';
    }
    return b;
}
const char *byte_to_binary64(__int64 x)
{
    static char b[65]; // bits plus '\0'
    b[0] = '\0';
    char *p = b;  

    unsigned __int64 z;
    for (z = 9223372036854775808; z > 0; z >>= 1)       //2^64
    {
        *p++ = (x & z) ? '1' : '0';
    }
    return b;
}

int floatToInt(float a)
{
    return (*((int*)&a));   
}

__int64 doubleToInt(double a)
{
    return (*((__int64*)&a));   
}
Run Code Online (Sandbox Code Playgroud)

输出

在此输入图像描述