Vim edit binaries-convert http://blog.csdn.net/hyhsousou/article/details/4786773

Source: Internet
Author: User

For binary file editing, Windows has a very good software winhex, which will make your life very simple. However, there seems to be no dedicated binary editor in Linux, but don't forget vim.
Although Vim is not designed for Binary editing, it also makes Vim a useful binary editor.
Now let's assume that I want to dump the first sector (the location of MBR and hard disk partition table) on the disk/dev/SDA and view and edit it.

View code bash

1
2
3
4
5
# Use dd to dump the data of the first 512byte of the hard disk
$ Sudo dd If =/dev/SDA of = SDA. mbr bs = 512 COUNT = 1
# Open it with vim with the-B parameter.-B tells Vim to open a binary file,
# Vim will not do some extra processing, such as automatic line feed.
$ Vim-B SDA. MBR

When you enter vim, garbled characters are displayed, so don't worry. Enter: %! In vim! Xxd:


0000000: eb48 90d0 bc00 7c8e c08e d8be 007c bf00 .H....|......|..
0000010: 06b9 0002 fcf3 a450 681c 06cb fbb9 0400 .......Ph.......
0000020: bdbe 0780 7e00 007c 0b0f 8510 0183 c510 ....~..|........
0000030: e2f1 cd18 8856 0055 c646 1105 c646 0302 .....V.U.F...F..
0000040: ff00 0020 0100 0000 0002 fa90 90f6 c280 ... ............
0000050: 7502 b280 ea59 7c00 0031 c08e d88e d0bc u....Y|..1......
0000060: 0020 fba0 407c 3cff 7402 88c2 52be 7f7d . ..@|<.t...R..}
0000070: e834 01f6 c280 7454 b441 bbaa 55cd 135a .4....tT.A..U..Z
0000080: 5272 4981 fb55 aa75 43a0 417c 84c0 7505 RrI..U.uC.A|..u.
0000090: 83e1 0174 3766 8b4c 10be 057c c644 ff01 ...t7f.L...|.D..
00000a0: 668b 1e44 7cc7 0410 00c7 4402 0100 6689 f..D|.....D...f.
00000b0: 5c08 c744 0600 7066 31c0 8944 0466 8944 /..D..pf1..D.f.D
00000c0: 0cb4 42cd 1372 05bb 0070 eb7d b408 cd13 ..B..r...p.}....
00000d0: 730a f6c2 800f 84ea 00e9 8d00 be05 7cc6 s.............|.
00000e0: 44ff 0066 31c0 88f0 4066 8944 0431 d288 D..f1...@f.D.1..
00000f0: cac1 e202 88e8 88f4 4089 4408 31c0 88d0 ........@.D.1...
0000100: c0e8 0266 8904 66a1 447c 6631 d266 f734 ...f..f.D|f1.f.4
0000110: 8854 0a66 31d2 66f7 7404 8854 0b89 440c .T.f1.f.t..T..D.
0000120: 3b44 087d 3c8a 540d c0e2 068a 4c0a fec1 ;D.}<.T.....L...
0000130: 08d1 8a6c 0c5a 8a74 0bbb 0070 8ec3 31db ...l.Z.t...p..1.
0000140: b801 02cd 1372 2a8c c38e 0648 7c60 1eb9 .....r*....H|`..
0000150: 0001 8edb 31f6 31ff fcf3 a51f 61ff 2642 ....1.1.....a.&B
0000160: 7cbe 857d e840 00eb 0ebe 8a7d e838 00eb |..}.@.....}.8..
0000170: 06be 947d e830 00be 997d e82a 00eb fe47 ...}.0...}.*...G
0000180: 5255 4220 0047 656f 6d00 4861 7264 2044 RUB .Geom.Hard D
0000190: 6973 6b00 5265 6164 0020 4572 726f 7200 isk.Read. Error.
00001a0: bb01 00b4 0ecd 10ac 3c00 75f4 c300 0000 ........<.u.....
00001b0: 0000 0000 0000 0000 3b8f 3a37 0000 8001 ........;.:7....
00001c0: 0100 83fe 3f3d 3f00 0000 7f32 0f00 0000 ....?=?....2....
00001d0: 013e 83fe ffff be32 0f00 79e7 9b03 00fe .>.....2..y.....
00001e0: ffff 83fe ffff 371a ab03 5d62 2719 00fe ......7...]b'...
00001f0: ffff 82fe ffff 947c d21c edc8 4900 55aa .......|....I.U.

In this way, it becomes a common binary editor. On the left is the data displayed in binary format, and on the right is the form of converting binary into characters. If the conversion fails, the characters are expressed as dots.
Note that if you want to modify a binary file, you must edit the hex on the left. It is useless to rewrite the text on the right. This is different from ultraedit.
After editing, click ": %! Xxd-R "Convert back to text mode": WQ ": the disk exits. If you do not have-R, VIM will save it in hex + text format on the screen. Remember to save it after-R.

Finally, let's summarize some methods for editing the binary file in VIM:
1. Use vim-B to open the file
2. Use: %! Xxd command
3. edit the file (only edit the right character will not save) and use: % xxd-R
4.: WQ save and exit the file

For a few .ow..exe files, you can also. the following code is added to vimrc. Vim automatically displays the file in binary mode. After editing, it can directly: WQ. Vim will automatically execute: % xxd-R and save and exit.

View code vi
1
2
3
4
5
6
7
8
9
10
augroup Binary
au!
au BufReadPre *.o,*.exe let &bin=1
au BufReadPost *.o,*.exe if &bin | %!xxd
au BufReadPost *.o,*.exe set ft=xxd | endif
au BufWritePre *.o,*.exe if &bin | %!xxd -r
au BufWritePre *.o,*.exe endif
au BufWritePost *.o,*.exe if &bin | %!xxd
au BufWritePost *.o,*.exe set nomod | endif
augroup END
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.