从二进制文件读取时将大尾数转换为小尾数 [英] Convert big endian to little endian when reading from a binary file

查看:257
本文介绍了从二进制文件读取时将大尾数转换为小尾数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找如何将big-endian转换为little-endian。但我没有找到任何好,可以解决我的问题。它似乎有很多方式你可以做这个转换。无论如何这个下面的代码在大端系统中正常工作。但是我应该怎么写一个转换函数,所以它将工作在小端系统以及?

I've been looking around how to convert big-endian to little-endians. But I didn't find any good that could solve my problem. It seem to be there's many way you can do this conversion. Anyway this following code works ok in a big-endian system. But how should I write a conversion function so it will work on little-endian system as well?

这是一个家庭作业,但它只是一个额外的学校系统运行big-endian系统。这只是我很好奇,想让它在我的家用电脑上工作

This is a homework, but it just an extra since the systems at school running big-endian system. It's just that I got curious and wanted to make it work on my home computer also

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
   ifstream file;

   file.open("file.bin", ios::in | ios::binary);

   if(!file)
      cerr << "Not able to read" << endl;
   else
   {
      cout << "Opened" << endl;

      int i_var;
      double d_var;

      while(!file.eof())
      {
         file.read( reinterpret_cast<char*>(&i_var) , sizeof(int) );
         file.read( reinterpret_cast<char*>(&d_var) , sizeof(double) );
         cout << i_var << " " << d_var << endl;
      }
   }
   return 0;
}

已解决

所以Big-endian VS Little-endian只是字节的逆序。这个功能我写的似乎服务于我的目的。我在这里添加它,以防其他人在将来需要它。这是双重的,虽然,对于整数使用函数torak建议或你可以修改这个代码,通过使它只交换4个字节。

So Big-endian VS Little-endian is just a reverse order of the bytes. This function i wrote seem to serve my purpose anyway. I added it here in case someone else would need it in future. This is for double only though, for integer either use the function torak suggested or you can modify this code by making it swap 4 bytes only.

double swap(double d)
{
   double a;
   unsigned char *dst = (unsigned char *)&a;
   unsigned char *src = (unsigned char *)&d;

   dst[0] = src[7];
   dst[1] = src[6];
   dst[2] = src[5];
   dst[3] = src[4];
   dst[4] = src[3];
   dst[5] = src[2];
   dst[6] = src[1];
   dst[7] = src[0];

   return a;
}


推荐答案

要继续,它是方便的保持一个帮助函数的小库文件。这些函数中的2个应该是4字节值的端序交换和2字节值。有关实例(包括代码),请查看本文

Assuming you're going to be going on, it's handy to keep a little library file of helper functions. 2 of those functions should be endian swaps for 4 byte values, and 2 byte values. For some solid examples (including code) check out this article.

一旦你得到了交换函数,任何时候在错误的字节序中读取一个值,调用适当的交换函数。有时候,这里的人们的一个绊脚是,单字节值不需要进行endian交换,因此,如果你正在读一个像字符流一样表示一个字符串的文件,这应该是好的。只有当你读取一个值,这是多个字节(像一个整数值),你必须交换他们。

Once you've got your swap functions, any time you read in a value in the wrong endian, call the appropriate swap function. Sometimes a stumbling point for people here is that single byte values do not need to be endian swapped, so if you're reading in something like a character stream that represents a string of letters from a file, that should be good to go. It's only when you're reading in a value this is multiple bytes (like an integer value) that you have to swap them.

这篇关于从二进制文件读取时将大尾数转换为小尾数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆