任何方式用小端程序读大端序数据? [英] Any way to read big endian data with little endian program?

查看:258
本文介绍了任何方式用小端程序读大端序数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

外部组为我提供了一个在Big Endian机器上编写的文件,他们还为文件格式提供了C ++解析器。

An external group provides me with a file written on a Big Endian machine, and they also provide a C++ parser for the file format.

我只能运行解析器在一个小端机上有没有办法读取文件使用他们的解析器,每次读取后不添加一个swapbytes()调用?

I only can run the parser on a little endian machine - is there any way to read the file using their parser without add a swapbytes() call after each read?

推荐答案

回到早期的铁器时代,Ancients遇到这个问题,当他们试图网络原始的PDP-11小型计算机与其他原始计算机。 PDP-11是第一个小端机的计算机,而当时大多数其他的是大端点。

Back in the early Iron Age, the Ancients encountered this issue when they tried to network primitive PDP-11 minicomputers with other primitive computers. The PDP-11 was the first little-Endian computer, while most others at the time were big-Endian.

为了解决这个问题,他们开发了网络字节顺序概念(总是大-Endia)和相应的网络字节顺序宏ntohs(),ntohl(),htons()和htonl()。用这些宏编写的代码总是得到正确的答案。

To solve the problem, once and for all, they developed the network byte order concept (always big-Endia), and the corresponding network byte order macros ntohs(), ntohl(), htons(), and htonl(). Code written with those macros will always "get the right answer".

精益在您的外部供应商在代码中使用宏,并且他们提供的文件将永远是big-endian,即使他们切换到一个little-endian机器。重写他们给你使用宏的解析器,并且你总是能够读取他们的文件,即使你切换到big-endian机器。

Lean on your external supplier to use the macros in their code, and the file they supply you will always be big-Endian, even if they switch to a little-Endian machine. Rewrite the parser they gave you to use the macros, and you will always be able to read their file, even if you switch to a big-Endian machine.

大量的程序员时间已经浪费在这个特定的问题上。有些日子,我认为一个好的论据可以作为悬挂PDP-11设计师谁做了小端的功能决定。

A truly prodigious amount of programmer time has been wasted on this particular problem. There are days when I think a good argument could be made for hanging the PDP-11 designer who made the little-Endian feature decision.

这篇关于任何方式用小端程序读大端序数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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