其他分享
首页 > 其他分享> > double,int等数据类型转换,传输,还原的示例

double,int等数据类型转换,传输,还原的示例

作者:互联网

double,int等数据类型转换,传输,还原的示例

#include <iostream>
using namespace std;
int main()
{
    double d1,d2,d3,d4;
    int i1,i2,i3,i4;
    unsigned char buf1[4]={0};
    unsigned char buf2[4]={0};
    d1=200000.3334;
    d2=-200000.3334;
    d3=d1*1000;d4=d2*1000;
    
    i1=(int)d3;
    i2=(int)d4;
    buf1[0]=i1>>24;
    buf1[1]=i1>>16;
    buf1[2]=i1>>8;
    buf1[3]=i1;
        buf2[0]=i2>>24;
    buf2[1]=i2>>16;
    buf2[2]=i2>>8;
    buf2[3]=i2;
    
    i3=buf1[0]<<24|buf1[1]<<16|buf1[2]<<8|buf1[3];
    i4=buf2[0]<<24|buf2[1]<<16|buf2[2]<<8|buf2[3];
    
    d3=(double)i3/1000;
    d4=(double)i4/1000;
    cout.precision(10); // 设置输出精度
        cout << "tranform test:" << endl;
    cout<<"d1:"<< d1 << endl;
    cout<<"d2:"<< d2 << endl;
    cout<<"i1:"<< i1 << endl;
    cout<<"i2:"<< i2 << endl;
    cout<<"i3:"<< i3 << endl;
    cout<<"i4:"<< i4 << endl;
    cout<<"d3:"<< d3 << endl;
    cout<<"d4:"<< d4 << endl;
    
    return 0;
}

运行结果:

tranform test:
d1:200000.3334
d2:-200000.3334
i1:200000333
i2:-200000333
i3:200000333
i4:-200000333
d3:200000.333
d4:-200000.333

标签:类型转换,buf2,示例,int,i2,i1,buf1,d4
来源: https://www.cnblogs.com/Baron-Lu/p/11806831.html