Hexdump Issue is Macos-Only
This problem used to be a hexdump bug on debian, but was fixed. I've tested this on FreeBSD, and it's fixed in the October 29, 2014
version of hexdump.
The version of BSD hexdump on Macos (see man hexdump
) has not been updated since July 10, 2004
and still has this problem. For more info on trying to find the version of Macos utilities, this SO question regarding sed is helpful.
hexdump
We can get around it by putting the \x
in a separate format string. For more information on why this is necessary, this SO question about escaping tabs is useful.
hexdump -v -e '"\\\x" 1/1 "%02x"' $file
Three backslashes are necessary to get a \
literal with x
following.
xxd
For sake of completeness, an xxd implementation takes more steps, but doesn't require formatting string shenanigans:
xxd -p $file | tr -d '\n' | sed 's/\(..\)/\\x\1/g'