Search code examples
text-processinghexdump

Escape percent sign (%) in hexdump format


I'm trying to emit a hex string like:

echo hello | hexdump -ve '/1 "_%02X"' ; echo

but with % instead.

echo hello | hexdump -ve '/1 "%%%02X"' ; echo

fails with

hexdump: bad conversion character %%

Is there any way to escape % in hexdump format string?


Solution

  • I don't see any way to get hexdump to emit a '%' character directly. Perhaps you could continue to emit the '_' character and then pipe the result through sed to convert the '_' into a '%'. Something like this:

    echo hello | hexdump -ve '/1 "_%02X"' | sed -e 's/_/%/g'
    

    which produces:

    %68%65%6C%6C%6F%0A