c++ float类型小端转大端存储
2017-06-06 10:47
155 查看
#include <iostream>
#include <string.h>
using namespace std;
typedef unsigned int UINT;
typedef unsigned char UCHAR;
typedef union{
float f;
char c[4];
}FLOAT_CONV;
static float __ltobf(float data)
{
FLOAT_CONV d1, d2;
d1.f = data;
d2.c[0] = d1.c[3];
d2.c[1] = d1.c[2];
d2.c[2] = d1.c[1];
d2.c[3] = d1.c[0];
cout<<"num:"<<hex<<d2.c<<endl;
return d2.f;
}
int main()
{
float d = 123;
float b = __ltobf(d);
int len = sizeof(float);
char *num = (char *) malloc (len);
memcpy(num, &b, len);
string a;
a.assign(num,len);
free(num);
cout<<"num:"<<hex<<a<<endl;
return 0;
}
#include <string.h>
using namespace std;
typedef unsigned int UINT;
typedef unsigned char UCHAR;
typedef union{
float f;
char c[4];
}FLOAT_CONV;
static float __ltobf(float data)
{
FLOAT_CONV d1, d2;
d1.f = data;
d2.c[0] = d1.c[3];
d2.c[1] = d1.c[2];
d2.c[2] = d1.c[1];
d2.c[3] = d1.c[0];
cout<<"num:"<<hex<<d2.c<<endl;
return d2.f;
}
int main()
{
float d = 123;
float b = __ltobf(d);
int len = sizeof(float);
char *num = (char *) malloc (len);
memcpy(num, &b, len);
string a;
a.assign(num,len);
free(num);
cout<<"num:"<<hex<<a<<endl;
return 0;
}
相关文章推荐
- 大端小端存储模式以及其对Union数据类型的影响
- C++ 判断是大端存储还是小端存储
- C++学习---一道笔试题:测试当前电脑系统是大端存储还是小端存储数据
- c\c++ 复习基础要点07---内存存储 大端模式、小端模式
- C语言——判断存储类型是大端还是小端
- C++Primer3.5标准库类型bitset---字节存储排序:大端和小端的判别及转换(转载)
- c/c++中float和double类型的存储
- C++中float类型的存储
- 大端小端存储模式以及其对Union数据类型的影响
- 浮点性(float)转化为字符串类型 自定义实现和深入探讨C++内部实现方法
- 【转】c++各种数据类型所占存储空间大小
- 大端与小端存储模式详解
- 大端格式和小端格式存储的区别
- 单精度类型(float)和双精度类型(double)存储
- C/C++中存储类型修饰符的区别(auto、static、register、extern、volatile、restrict)
- 存储格式之大端小端详解
- 端小端存储模式以及其对Union数据类型的影响
- c++各种数据类型所占存储空间大小
- C++存储类型
- c++ float类型研究