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

Akr picture Akr · Aug 22, 2013 · Viewed 30.6k times · Source

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.

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:

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:

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.

Thank you for any explainations!

Answer

JScoobyCed picture JScoobyCed · Aug 22, 2013

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

xxd mbr

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

vi -b mrb

then :%!xxd