Skip to content

Instantly share code, notes, and snippets.

@forflo
Last active August 29, 2015 14:14
Show Gist options
  • Save forflo/b54f94ea96d51ef56ece to your computer and use it in GitHub Desktop.
Save forflo/b54f94ea96d51ef56ece to your computer and use it in GitHub Desktop.
Hexdump
##
## 1) Offset is displayed in Hex. 4 Bytes are interpreted at
## once plus each byte is displayed in ASCII with the char "."
## representing every non-printing character. Since my current
## computer runs an intel processor (little endian) the byte order
## is reverse to the hex depiction.
##
hexdump -e '1/4 "[offset:%_ax]) "' -e '1/4 "%08X <-> "' -e '4/1 "%_p "' -e '"\n"'
# example of 1:
$ hexdump -e '1/4 "[offset:%_ax]) "' -e '1/4 "%08X <-> "' -e '4/1 "%_p "' -e '"\n"' < test.foo
[offset:578]) 00000000 <-> . . . .
[offset:57c]) 81462376 <-> v # F .
##
## 2) No Offset. 8 single bytes are interpreted as octal numbers and
## also as ASCII chars with "." displaying every non-printable char
##
hexdump -e '8/1 "%o "' -e '"\t\t"' -e '8/1 "|%_p"' -e '"|\n"'
##
# 2.1) With offset (octal)
# The rest is equivalent to 2
##
hexdump -e '"[offset:%6_ao] "' -e '8/1 "%o "' -e '"\t\t"' -e '8/1 "%_p"' -e '"\n"'
# example of 2.1:
$ >> cat test.txt
abcdeff
awreawi
aweraeo
foofoou
$ >> hexdump -e '"[offset:%6_ao] "' -e '8/1 "%o "' -e '"\t\t"' -e '8/1 "%_p"' -e '"\n"' test.txt
[offset: 0] 141 142 143 144 145 146 146 12 abcdeff.
[offset: 10] 141 167 162 145 141 167 151 12 awreawi.
[offset: 20] 141 167 145 162 141 145 157 12 aweraeo.
[offset: 30] 146 157 157 146 157 157 165 12 foofoou.
##
## 3) Offset in hex, padding to 6 chars with "0" filling.
## Outputs three columns with different kinds of data representation
## The first one displays the 4 bytes exactly as they are read from the input
## (useful for determining the endianness of the executing computer), the
## second displays the data as uint32 in hex and the last column shows just
## signed 32 bit integers padded to fit inside of an eleven-char array.
##
hexdump -e '"%06_ax"' -e '" [sequential]-> "' -e '4/1 "%2x "' -e '" [uint32,hex]-> "' -e '/4 "%08X "' -e '" [sint32,dec]-> "' -e '/4 "%011i \n"'
# example of 3:
$ >> $commandlinefoo | hexdump -e '"%06_ax"' -e '" [sequential]-> "' -e '4/1 "%2x "' -e '" [uint32,hex]-> "' -e '/4 "%08X "' -e '" [sint32,dec]-> "' -e '/4 "%011i \n"'
000000 [sequential]-> 0 0 0 0 [uint32,hex]-> 00000000 [sint32,dec]-> 00000000000
*
000400 [sequential]-> 40 0 0 0 [uint32,hex]-> 00000040 [sint32,dec]-> 00000000064
000404 [sequential]-> e8 1 0 0 [uint32,hex]-> 000001E8 [sint32,dec]-> 00000000488
000408 [sequential]-> 18 0 0 0 [uint32,hex]-> 00000018 [sint32,dec]-> 00000000024
00040c [sequential]-> cc 1 0 0 [uint32,hex]-> 000001CC [sint32,dec]-> 00000000460
000410 [sequential]-> 35 0 0 0 [uint32,hex]-> 00000035 [sint32,dec]-> 00000000053
000414 [sequential]-> 1 0 0 0 [uint32,hex]-> 00000001 [sint32,dec]-> 00000000001
000418 [sequential]-> 0 0 0 0 [uint32,hex]-> 00000000 [sint32,dec]-> 00000000000
*
[and so forth...]
## you may have noticed that the sequential bytes are written in lower case hex letters.
## To change that (for cosmetic reasons) you would have to replace the lower case "x" with
## an upper case one (it's located here: ... [sequential]-> "' -e '4/1 "%2x <- this x! ...)
#############################
# Bash-Aliases #
#############################
# alias for 1
alias "hexd-cus1"="hexdump -e '1/4 \"[offset:%_ax]) \"' -e '1/4 \"%08X <-> \"' -e '4/1 \"%_p \"' -e '\"\n\"'"
# alias for 3
alias "hexd-cus2"="hexdump -e '\"%06_ax\"' -e '\" [sequential]-> \"' -e '4/1 \"%2X \"' -e '\" [uint32,hex]-> \"' -e '/4 \"%08X \"' -e '\" [sint32,dec]-> \"' -e '/4 \"%011i \n\"'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment