拜托交换帮助请 [英] Byte swapping help please

查看:78
本文介绍了拜托交换帮助请的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在另一台机器上打开一个看起来像0xABCDEF01的文件,但在我的机器上打开了
0x01EFCDAB。


这是一个字节交换吗?


任何人都可以给出一个检查字节是否被交换的好方法吗? (代码

应该可以在不同的机器上顺利运行。)


谢谢,

Ann

I am opening a file which looks like 0xABCDEF01 on another machine but
0x01EFCDAB on my machine.

Is this a byte swapping?

Could anyone give a good way to check if bytes are being swapped? (code
should work smoothly across different machine.)

Thanks,
Ann

推荐答案

在文章< 11 ********************** @ z34g2000cwc.googlegroups .com> ,

Ann< ps **** @ yahoo.com>写道:
In article <11**********************@z34g2000cwc.googlegroups .com>,
Ann <ps****@yahoo.com> wrote:
我在另一台机器上打开一个看起来像0xABCDEF01但在我的机器上是0x01EFCDAB的文件。

这是一个字节交换吗?


或者可能配偶交换。

有没有人能给出一个检查字节是否被交换的好方法? (代码
应该可以在不同的机器上顺利运行。)
I am opening a file which looks like 0xABCDEF01 on another machine but
0x01EFCDAB on my machine.

Is this a byte swapping?
Or possbily spouse swapping.
Could anyone give a good way to check if bytes are being swapped? (code
should work smoothly across different machine.)




也许我们应该直接将代码发送给你的导师所以

我们可以为你的作业取得好评吗?

-


Craig Ruff NCAR cr *** @ ucar.edu

303-497-1211 PO Box 3000

Boulder,CO 80307



Perhaps we should just send the code directly to your instructor so
we can get the credit for your homework?
--

Craig Ruff NCAR cr***@ucar.edu
303-497-1211 P.O. Box 3000
Boulder, CO 80307


" Ann" < PS **** @ yahoo.com>写道:
"Ann" <ps****@yahoo.com> writes:
我在另一台机器上打开一个看起来像0xABCDEF01的文件但在我的机器上是0x01EFCDAB。

这是一个字节交换吗?


看起来像。

有没有人能给出一个检查字节是否被交换的好方法? (代码
应该可以在不同的机器上顺利运行。)
I am opening a file which looks like 0xABCDEF01 on another machine but
0x01EFCDAB on my machine.

Is this a byte swapping?
Looks like it.
Could anyone give a good way to check if bytes are being swapped? (code
should work smoothly across different machine.)




原则上,没有可靠的方法可以说明。如果从文件中读取32位
无符号整数并获得值0xABCDEF01,那么如何知道它是否应该是0xABCDEF01或0x01EFCDAB?没有更多

的信息,你不可能。即使有更多信息,您也可能无法告诉他们。


如果您将二进制数据存储在文件中,则字节顺序只有一个

您可能遇到的问题。类型的大小可以根据不同的实现而变化。所以cah浮点表示。


最安全的方法是只写*字节数据。例如,如果你想要将一个整数值0x01EFCDAB写入一个文件,你可以读取和/或
写入单个字节(0x01,0xEF,0xCD,0xAB)一个固定的订单。

或者你可以写一个数字的文字表示,这也是

的优点是让你用文本编辑器查看文件。


严格来说,在字节大于8位的字节大小或非ASCII字符集的系统上,您可能仍会遇到问题;前者

在实践中不太可能出现,而后者可以用

文本转换工具解决。 (有些系统,主要是DSP,字节大于8位,但是它们是嵌入式系统,而你可能不需要分享b $ b b)与它们一起使用的文件。)


如果必须将原始二进制数据写入文件,可以在文件头中添加信息

,指示数据的格式化方式。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



In principle, there is no reliable way to tell. If you read a 32-bit
unsigned integer from a file and get a value of 0xABCDEF01, how can
you know whether it should be 0xABCDEF01 or 0x01EFCDAB? Without more
information, you can''t. Even with more information, you may not be
able to tell.

If you''re storing binary data in a file, byte ordering is only one of
the problems you can run into. Sizes of types can vary across
different implementations; so cah floating-point representations.

The safest approach is to write *only* byte data. For example, if you
want to write an integer value 0x01EFCDAB to a file, you can read and
write the individual bytes (0x01, 0xEF, 0xCD, 0xAB) in a fixed order.
Or you can write a textual representation of the number, which also
has the advantage of letting you view the file with a text editor.

Strictly speaking, you might still have problems on systems with byte
sizes bigger than 8 bits, or with non-ASCII character sets; the former
is unlikely to arise in practice, and the latter can be solved with
textual conversion tools. (There are systems, mostly DSPs, with bytes
bigger than 8 bits, but they''re embedded systems, and you''re not
likely to need to share files with them.)

If you must write raw binary data to a file, you might add information
to the file header indicating how the data is formatted.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


cr *** @ ucar.edu (Craig Ruff)写道:
cr***@ucar.edu (Craig Ruff) writes:
在文章< 11 ********************** @ z34g2000cwc.googlegroups .com>中,
Ann< ps **** @ yahoo.com>写道:
In article <11**********************@z34g2000cwc.googlegroups .com>,
Ann <ps****@yahoo.com> wrote:
我在另一台机器上打开一个看起来像0xABCDEF01但在我的机器上是0x01EFCDAB的文件。

这是一个字节交换吗?
I am opening a file which looks like 0xABCDEF01 on another machine but
0x01EFCDAB on my machine.

Is this a byte swapping?



或者可能配偶交换。



Or possbily spouse swapping.

有没有人能给出一个检查字节是否被交换的好方法? (代码
应该可以在不同的机器上顺利运行。)
Could anyone give a good way to check if bytes are being swapped? (code
should work smoothly across different machine.)



或许我们应该直接将代码发送给您的教师,以便我们可以为您的作业获得荣誉?



Perhaps we should just send the code directly to your instructor so
we can get the credit for your homework?




我没有看到任何迹象表明这是作业。 (如果是的话,它是非常糟糕的问题;因为我提到了elsethread,没有

可靠的方法来检测二进制文件的字节顺序。 )


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



I didn''t see any indication that this was homework. (If it was, it''s
a very poorly stated problem; as I mentioned elsethread, there is no
reliable way to detect the byte ordering of a binary file.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于拜托交换帮助请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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