Chinaunix首页 | 论坛 | 博客
  • 博客访问: 575233
  • 博文数量: 112
  • 博客积分: 5090
  • 博客等级: 大校
  • 技术积分: 1158
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-19 22:21
文章分类

全部博文(112)

文章存档

2016年(1)

2015年(1)

2012年(1)

2011年(5)

2010年(7)

2009年(6)

2008年(18)

2007年(28)

2006年(16)

2005年(29)

我的朋友

分类: LINUX

2011-08-11 20:31:28

Every time I go to use hexdump I find myself stumbling over it's bizarre argument format and ineffectual manual. Because I look at binary files often I decided to work up this page more for myself than anyone else.

Here is a sample usage of hexdump:

cat file | hexdump -v -e '"\\\x" 1/1 "%02x" " "'

And here is some sample output from it:

\x97\x6d\xea\xd1\x21\x02\x12\x80\x01\x9d...
Argument Description

The -v argument is needed to not print duplicate chars as a *. The -e argument has 5 parts separated by whitespace. I'm not sure how multiple format entries interact.

  1. An optional starting string (in quotes) which is printed before each conversion. In the first example below we are printing x before each hex character ("x").
  2. An interation count which defaults to 1 if not supplied but has to be supplied if you want a byte count. This tells how many times to do the conversion before we print the end string. So if you were decoding 4 things, each of 1 byte, you'd say 4/1.
  3. A byte count which is separated from the interation count by a /. I don't think there is a way to specify a byte count without an interation count. This specifies how many bytes are in each item that is being decoded.
  4. A sprintf format. The manual is somewhat useful here. Some standard ones apply and there are _ extensions. In the first example below this is "%02X" which prints each byte as a 2 character, 0 prefixed capital hex number.
  5. An optional ending string (in quotes) which is printed after the conversion. A space in the first example below (" ").
Format Examples

Print an "x" to start, take 1 item / 1 byte, print it as a 2 digit 0 prefixed hex value ("%02X"), and end with a space.

echo "hello there" | hexdump -v -e '"x" 1/1 "%02X" " "'

Outputs something like:

x68 x65 x6C x6C x6F x20 x74 x68 x65 x72 x65 x0A

Print an "[" to start, take 2 items / each 1 byte, print them as a 3 digit 0 prefixed octal value followed by a space ("%03o "), and end with "]\n".

echo "hello there" | hexdump -v -e '"[" 2/1 "%03o " "]\n"'

Outputs something like:

[150 145] [154 154] [157 040] [164 150] [145 162] [145 012]

NOTE: the trailing space in "%03o " format will not be printed at the end of each interation.



阅读(659) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~