Linux 用vi和hexedit打开一个二进制文件,为什么内容不一样?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18370576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:37:56  来源:igfitidea点击:

Open a binary file using vi and hexedit, why are the contens different?

linuxvibinaryfileshex-editorsmbr

提问by Akr

I'm trying to edit a binary file directly and I know two editors, vi and hexedit. But when I open a binary file separately using them, the cotens are different. Below is what I did.

我想直接编辑一个二进制文件,我知道两个编辑器,vi 和 hexedit。但是当我使用它们单独打开一个二进制文件时,cotens 是不同的。下面是我所做的。

First I use "dd if=/dev/sda of=mbr bs=512 count=1" to generate the binary file, which contains the mbr data. Then I open it using "hexedit mbr", and it displays this: beginning:

首先我使用“dd if=/dev/sda of=mbr bs=512 count=1”来生成包含mbr数据的二进制文件。然后我使用“hexedit mbr”打开它,它显示:开始:

00000000   EB 63 90 D0  BC 00 7C 8E  C0 8E D8 BE  00 7C BF 00 
00000010   06 B9 00 02  FC F3 A4 50  68 1C 06 CB  FB B9 04 00
00000020   BD BE 07 80  7E 00 00 7C  0B 0F 85 0E  01 83 C5 10

ending:

结尾:

000001E0   FF FF 83 FE  FF FF 00 40  D6 02 00 38  2B 01 00 00 
000001F0   00 00 00 00  00 00 00 00  00 00 00 00  00 00 55 AA

I using "vi mbr" open it and type":%!xxd", it displays this: beginning:

我使用“vi mbr”打开它并输入“:%!xxd”,它显示:开始:

0000000: c3ab 63c2 90c3 90c2 bc00 7cc2 8ec3 80c2
0000010: 8ec3 98c2 be00 7cc2 bf00 06c2 b900 02c3 
0000020: bcc3 b3c2 a450 681c 06c3 8bc3 bbc2 b904

ending:

结尾:

00002b0: bfc3 bf00 40c3 9602 0038 2b01 0000 0000 
00002c0: 0000 0000 0000 0000 0000 0000 55c2 aa0a

The hexedit displaying is what I expect in mbr. But what to say with vi displaying? Also the vi displaying seems wrong because there are more than 512 bytes.

hexedit 显示是我在 mbr 中所期望的。但是vi显示怎么办?此外,vi 显示似乎是错误的,因为有超过 512 个字节。

Thank you for any explainations!

感谢您的任何解释!

采纳答案by JScoobyCed

The command :%!xxduses the external program xxd, so you should first try to check its output by typing:

该命令:%!xxd使用外部程序xxd,因此您应该首先尝试通过键入以下内容来检查其输出:

xxd mbr

If it looks good, try opening vi in binary mode (no EOL):

如果看起来不错,请尝试以二进制模式打开 vi(无 EOL):

vi -b mrb

then :%!xxd

然后 :%!xxd

回答by Martin Rosenau

The bytes you see in "vi" seem to be exactly the UTF-8 representation of the binary code.

您在“vi”中看到的字节似乎正是二进制代码的 UTF-8 表示。

Maybe "vi" converts the data read in from binary to UTF-8 before passing it to "xxd".

也许“vi”在将它传递给“xxd”之前将从二进制读取的数据转换为UTF-8。