(三十)C++篇-float转byte

2022-10-28  本文已影响0人  GoodTekken

float型在线转换工具--在线进制转换.
之前看了一篇文章,上面写浮点数值搭配通讯…自动化工程师难跨越的门槛,既然这个门槛不容易越,我们不妨越一下,以下总结三种方法(联合体,指针,位运算):

  1. 联合体方法,自动转换
struct data
{
    union
    {
        float float_value;
        unsigned char byte_value[4];
    };
}float_data;
  1. 指针方法(1)--指针地址自增
bool float_to_byte_2(float float_value,unsigned char byte[])
{   
    unsigned char* pdata = (unsigned char*)&float_value;  
    for(int i=0;i<4;i++)  
    {  
        byte[i] = *pdata++;//指针地址自增     
    }  
    return true;
}
  1. 指针方法(2)--指针地址索引
bool float_to_byte_3(float float_value,unsigned char byte[])
{   
    unsigned char* pdata = (unsigned char *)&float_value;
    for(int i=0;i<4;i++)  
    {  
        byte[i] = *(pdata+i); //指针地址索引
    }  
    return true;
}
  1. 传统的float数值与位变换
bool float_to_byte_4(float float_value,unsigned char byte[])
{   
    bool convert_bit[32];
    float plus_float = float_value;
    float temp_float;
    
    ......

    return true;
}

以下是代码的测试实例,其中有好玩的16进制输出格式“printf”和“cout”,按照自己喜欢的格式输出结果即可。

测试代码:

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include<cmath>

using namespace std;

//Method 1 联合体方法,自动转换
struct data
{
    union
    {
        float float_value;
        unsigned char byte_value[4];
    };
}float_data;


//Method 2 float型数据与字节数组的转化
bool float_to_byte_2(float float_value,unsigned char byte[])
{   
    unsigned char* pdata = (unsigned char*)&float_value;  
    for(int i=0;i<4;i++)  
    {  
        byte[i] = *pdata++;//指针地址自增     
    }  
    return true;
}

//Method 3 float型数据与字节数组的转化
bool float_to_byte_3(float float_value,unsigned char byte[])
{   
    unsigned char* pdata = (unsigned char *)&float_value;
    for(int i=0;i<4;i++)  
    {  
        byte[i] = *(pdata+i); //指针地址索引
    }  
    return true;
}

//Method 4 传统的float数值与位变换
bool float_to_byte_4(float float_value,unsigned char byte[])
{   
    bool convert_bit[32];
    float plus_float = float_value;
    float temp_float;
    
    int e = 127;
    int a = 0;
    //1,sign
    (float_value>=0)?convert_bit[31] = 0 : convert_bit[31] = 1;
    (float_value>=0)?plus_float = float_value : plus_float=-float_value;

    if(float_value == 0)
    {
        byte[0] = 0;
        byte[1] = 0;
        byte[2] = 0;
        byte[3] = 0;
        return true;
    }   
    
    temp_float = plus_float;
    while(temp_float >= 2)
    {
        temp_float = temp_float/2.0;
        a = a + 1;
    }
    temp_float = plus_float;

    while(temp_float < 1)
    {
        temp_float = temp_float*2.0;
        a = a - 1;
    }
    e = e + a;
    for(int i = 0;i<8;i++)
    {
        int t = 23 + i;
        convert_bit[t] = (e>>i)&0x01;
    }
    
    //3,fraction

    temp_float = plus_float * pow(2,-a);
    temp_float = temp_float - 1;
    for(int i = 22;i>=0;i--)
    {
        temp_float = temp_float *2;
        if(temp_float >= 1)
        {
            temp_float = temp_float -1;
            convert_bit[i] = 1;
        }
        else
        {
            convert_bit[i] = 0;
        }
    }
    
    for(int i = 0;i<8;i++)
    {
        byte[0] += convert_bit[i]<<i;
    }

    for(int i = 8;i<16;i++)
    {
        byte[1] += convert_bit[i]<<(i-8);
    }

    for(int i = 16;i<24;i++)
    {
        byte[2] += convert_bit[i]<<(i-16);
    }

    for(int i = 24;i<32;i++)
    {
        byte[3] += convert_bit[i]<<(i-24);
    }
    return true;
}


int main()
{
    float_data.float_value = 1.234;
    cout<<"float:"<<float_data.float_value<<endl;
    cout<<"Method 1:"<<endl;
    for(int i = 0;i<4;i++)
    {
        printf("byte%d:%d  ", i,float_data.byte_value[i]);
    }
    cout<<endl;
    for(int i = 0;i<4;i++)
    {
        printf("byte%d:%x  ", i,float_data.byte_value[i]);
    }
    cout<<endl;
    for(int i = 0;i<4;i++)
    {
        cout.setf(ios::hex);
        cout<< hex<<"byte"<<i<<":"<<(int)float_data.byte_value[i]<<"  "; 
    }
    cout<<endl;
    
    cout<<"Method 2:"<<endl;
    unsigned char byte[4] = {0,0,0,0};
    float_to_byte_2(1.234,byte);
    for(int i = 0;i<4;i++)
    {
        cout.setf(ios::hex);
        cout<< hex<<"byte"<<i<<":"<<(int)byte[i]<<"  "; 
    }
    cout<<endl;

    cout<<"Method 3:"<<endl;
    unsigned char byt3[4] = {0,0,0,0};
    float_to_byte_3(1.234,byt3);
    for(int i = 0;i<4;i++)
    {
        cout.setf(ios::hex);
        cout<< hex<<"byte"<<i<<":"<<(int)byt3[i]<<"  "; 
    }
    cout<<endl;
    
    cout<<"Method 4:"<<endl;
    unsigned char byt4[4] = {0,0,0,0};  
    float_to_byte_4(1.234,byt4);
    for(int i = 0;i<4;i++)
    {
        cout.setf(ios::hex);
        cout<< hex<<"byte"<<i<<":"<<(int)byt4[i]<<"  "; 
    }
    cout<<endl;
    
    return 0;
}

输出结果:

float:1.234
Method 1:
byte0:182  byte1:243  byte2:157  byte3:63  
byte0:b6  byte1:f3  byte2:9d  byte3:3f  
byte0:b6  byte1:f3  byte2:9d  byte3:3f  
Method 2:
byte0:b6  byte1:f3  byte2:9d  byte3:3f  
Method 3:
byte0:b6  byte1:f3  byte2:9d  byte3:3f  
Method 4:
byte0:b6  byte1:f3  byte2:9d  byte3:3f 
上一篇下一篇

猜你喜欢

热点阅读