交换浮点数组中的字节
swapping bytes in float array
我目前正在研究一种二进制文件格式,其中数据表示为浮点数数组,并且数据始终应该使用小端表示形式写入。所以,目前我做以下事情:
float * swapped_array = new float[length_of_array];
for (int i = 0; i < length_of_array; ++i) {
swapped_array[i] = swap_float(input_array[i]);
}
在这里,swap_float交换浮点值的四个字节。现在,我想知道是否有一种方法可以跨平台的方式做到这一点,而无需迭代使用它 for 循环并使其计算效率更高。
在我看来,
您可以使用一些指针算法来交换字节:
byte mem;
byte* first = (byte*) floatpointer;
mem = *first;
*first = *(first+0x03);
*(first+0x03) = mem;
first++;
mem = *first;
*first = *(first+0x01);
*(first+0x01) = mem;
相关文章:
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 基于字节数组生成静态范围整数值
- 将字节数组转换为带有字节序问题的指针
- 使用 stbi_write_png,如何将 0 和 1 的矩形字节数组转换为单色 png 文件?
- 字节数组初始化会导致 DirectX 崩溃
- 使用 swig 追加到字节数组
- C++ 替换字节数组中项的顺序
- 如何从保存在 Java 中C++的字节数组中读取数字?
- 如何在C++(Arduino)中将浮点数组转换为字节数组
- 将字节数组转换为多维数组
- 如何将带有空字符的字节数组馈送到 std::iostream 中?
- C++字节数组到整数
- 将字符串转换为无符号字符数组/字节数组
- 用于 progmem 的C++和头文件压缩的 Web 文件字节数组
- 从 UTF-8 字节数组创建字符串?
- 如何将 CString 转换为字节 [] 数组?
- 使用 ESP8266/8285 将字符串从 Web 服务器转换为字节数组
- 如何将 (serverClients[i].read() 传递给 ESP8266 上的字节数组
- C++浮点数和字节数组的联合问题
- 如何初始化字节数组的映射?