Created
July 14, 2013 14:02
-
-
Save atupal/5994376 to your computer and use it in GitHub Desktop.
linux下将文件转化为16进制。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
linux中有多种方式可以将文件dump成16进制显示,也可以将16进制值再反向成文件。 | |
$ hexdump test.txt | |
0000000 524f 2d41 3030 3036 0a30 524f 2d41 3030 | |
0000010 3630 0a30 | |
0000014 | |
$ od -x test.txt | |
0000000 524f 2d41 3030 3036 0a30 524f 2d41 3030 | |
0000020 3630 0a30 | |
0000024 | |
$ xxd test.txt | |
0000000: 4f52 412d 3030 3630 300a 4f52 412d 3030 ORA-00600.ORA-00 | |
0000010: 3036 300a 060. | |
注意到hexdump和od出来的结果都是按实际存储的字节序,因为基于x86的linux是little-endian的,也就是高低字节是颠倒了的。但是xxd的结果是将字节序调整过了的。而IBM的Power CPU是big-endian的,所以在AIX上od的结果如下: | |
$od -x test.txt | |
0000000 4f52 412d 3030 3630 300a 4f52 412d 3030 | |
0000020 3036 300a | |
0000024 | |
xxd还可以实现从16进制反向生成文件,只需要加上-r选现即可。 | |
xxd test.txt | xxd -r | |
ORA-00600 | |
ORA-00060 | |
$ echo 0000000: 4f52 412d 3030 3630 300a 4f52 412d 3030 | xxd -r | |
ORA-00600 | |
ORA-00 | |
但是要注意xxd接受的是big-endian格式的16进制值,如果输入的是little-endian的,则生成的文件字符(包括换行符)是两两颠倒的。 | |
$ od -x test.txt | xxd -r | |
RO-A0006 | |
0RO-A0060 | |
0 | |
在vi编辑器中可以使用:%!xxd 调用xxd来将文件转换成16机制编辑模式,编辑完成后再调用:%!xxd -r转换文件模式,从而使得vi具有16进制编辑的功能,:%!其实就是调用外部shell命令,需要注意的是xxd的字节序是big-endian的,不要搞错了。 | |
如果你的Linux系统中找不到xxd命令,那么检查下是否有安装vim-common包 | |
rpm -qa | grep vim | |
vim-enhanced-6.3.046-0.40E.7 | |
vim-X11-6.3.046-0.40E.7 | |
vim-minimal-6.3.046-0.40E.7 | |
vim-common-6.3.046-0.40E.7 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment