hexdump related.

hexdump format strings

Tue 13 December 2005

In tips.

Ian Wienand

More from the "things you'd learn if you read the manual" department, although the manual as currently written is a little esoteric on this feature of hexdump.

$ hexdump -e ' [iterations]/[byte_count] "[format string]" '

As you might guess, this means apply format string to groups of byte_count bytes, iterations times. Format string is likeprintf.

You can of course chain multiple formats together, or put them in a file. So say you needed to clag some binary data into a C array, a-la firmware for loading into a driver. You could use

$ hexdump -v -e '6/4 "0x%08x, "' -e '"
"' ./dump

to get something that fits in 80 columns and is almost ready to go.

 

Manual on How to Use the Hexdump Unix Utility

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:

x97x6dxeaxd1x21x02x12x80x01x9d...

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 "] ".

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

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.

原文地址:https://www.cnblogs.com/yangyingchao/p/3233031.html