将双重价值从小恩迪安转换为大恩迪安
Convert double value from little endian to Big endian
本文关键字:转换 更新时间:2023-10-16
我正在从小恩迪安转换为大恩迪安,然后回到小检查
是对还是错,请帮助我
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
double dx = 122992001003000;
//A method little to big endian
double bEndian(double d) {
int *p0 = (int *) &d;
int *p1 = p0 + 1;
printf("1: %x %xn", *p0, *p1);
int tmp = *p1;
*p1 = htonl(*p0);
*p0 = htonl(tmp);
printf("2: %x %xn", *p0, *p1);
return *(double *)p0;
}
//A method big to little endian
double lEndian(double d) {
int *p0 = (int *) &d;
int *p1 = p0 + 1;
printf("3: %x %xn", *p0, *p1);
int tmp = *p1;
*p1 = ntohl(*p0);
*p0 = ntohl(tmp);
printf("4: %x %xn", *p0, *p1);
return *(double *)p0;
}
int main (int argc, char *argv[])
{
double d1 = bEndian(dx);
double d2 = lEndian(d1);
printf("5: %.0fn", d2);
if ((*(double*) &d2) == dx)
printf("6: %.0lfn", *(double*) &d2);
else
printf("7:%dn", d2);
return 0;
}
有时给我结果122992001003129为什么? 同样使用NTOHL或HTONL没有差异?仅是为了惯例吗即网络主机和主机网络
这是一个可以改变dendianness的函数。
#include <algorithm>
#include <cstdio>
template <class T>
T change_endian(T in)
{
char* const p = reinterpret_cast<char*>(&in);
for (size_t i = 0; i < sizeof(T) / 2; ++i)
std::swap(p[i], p[sizeof(T) - i - 1]);
return in;
}
int main()
{
double d = 122992001003000;
printf("%fn", d);
d = change_endian(d);
printf("%fn", d);
d = change_endian(d);
printf("%fn", d);
}
输出:
122992001003000.000000
0.000000
122992001003000.000000
Easy:
#include <endian.h>
#include <stdint.h>
double src_num = YOUR_VALUE;
int64_t tmp_num = htobe64(le64toh(*(int64_t*)&src_num));
double dst_num = *(double*)&tmp_num;
但是,有某些怪癖:https://en.wikipedia.org/wiki/endianness#floating_point_point
typedef union
{
double d;
unsigned char s[8];
} Union_t;
// reverse little to big endian or vice versa as per requirement
double reverse_endian(double in)
{
int i, j;
unsigned char t;
Union_t val;
val.d = in;
// swap MSB with LSB etc.
for(i=0, j=7; i < j; i++, j--)
{
t = val.s[i];
val.s[i] = val.s[j];
val.s[j] = t;
}
return val.d;
}
四个字节的简单反转应起作用
double ReverseDouble( const double indouble )
{
double retVal;
char *doubleToConvert = ( char* ) & indouble;
char *returndouble = ( char* ) & retVal;
// swap the bytes into a temporary buffer
returndouble[0] = doubleToConvert[3];
returndouble[1] = doubleToConvert[2];
returndouble[2] = doubleToConvert[1];
returndouble[3] = doubleToConvert[0];
return retVal;
}
相关文章:
- 防止主数据类型C++的隐式转换
- 模板参数替换失败,并且未完成隐式转换
- 努力将整数转换为链表。不知道我在这里做错了什么
- HEX值到wchar_t字符(UTF-8)的转换
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将 Qvector<uint8_t> 转换为 QString
- 如何在cuSparse中使用cusparseXcoo2csr从coo转换为csc
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 在c++中使用nlohmann从类到json的转换
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 将Integer转换为4字节的unsined字符矢量(按大端字节顺序)
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- 复制列表初始化的隐式转换的等级是多少
- 正在将指针转换为范围
- 如何防止 c++ 在从浮点型转换为双精度型(不适用于 IO)时添加额外的小数?
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 是否可以从int转换为enum类类型
- 了解 GLM- openGL 中的相机转换
- 将无符号char*转换为std::istream*C++