代码改变世界

linux hexdump使用

2019-09-23 16:39  abce  阅读(1474)  评论(0编辑  收藏  举报
# hexdump -h
hexdump: invalid option -- 'h'

Usage:
 hexdump [options] file...

Options:
 -b              one-byte octal display#单字节八进制显示
 -c              one-byte character display#单字节字符显示
 -C              canonical hex+ASCII display#规范化 十六进制+ASCII 显示
 -d              two-byte decimal display#两字节十进制显示
 -o              two-byte octal display#两字节八进制显示
 -x              two-byte hexadecimal display#两字节十六进制显示
 -e format       format string to be used for displaying data#格式 用于显示数据的格式字符串
 -f format_file  file that contains format strings#格式文件 包含格式字符串的文件
 -n length       interpret only length bytes of input#长度 只解释输入的指定长度个字节
 -s offset       skip offset bytes from the beginning#偏移 跳过开头指定长度个字节
 -v              display without squeezing similar lines#显示时不压缩相似的行
 -V              output version information and exit#显示此帮助并退出

  

# more a.txt 
abcde
ABCDE
# hexdump a.txt 
0000000 6261 6463 0a65 4241 4443 0a45          
000000c

  

第一列表示:文件偏移量
第二列表示:以两个字节为一组的十六进制
上面的输出结果翻译一下,就是:

badc0aeBADC0aE

(注意:在Linux中换行符\n 的十六进制为0a,在windows中,换行为\r\n的十六进制编码为:0d 0a)

为什么翻译成文本成倒序了呢?

其实这是CPU架构所致,感兴趣的可以看下大小端的定义:
1) Little-Endian就是低位字节排放在内存的低地址端,高位字节排放在内存的高地址端。(X86 CPU系列采用的位序)
2) Big-Endian就是高位字节排放在内存的低地址端,低位字节排放在内存的高地址端。
3) 网络字节序:TCP/IP各层协议将字节序定义为Big-Endian,因此TCP/IP协议中使用的字节序通常称之为网络字节序。

有没有更加较便于方便的查看方式了?有,这也是较常用的方式。
以16进制和相应的ASCII字符显示文件里的字符:

# hexdump -C a.txt 
00000000  61 62 63 64 65 0a 41 42  43 44 45 0a              |abcde.ABCDE.|
0000000c