10

If I opened hexdump without any argument in the terminal:

hexdump

When I type something in the terminal and press Enter, hexdump will display the hex values for whatever characters I type.

But hexdump will only display the hex values if I type 16 characters, for example:

enter image description here

Here, I typed the character a 15 times and I pressed Enter(so hexdump received 16 characters (15a + \n)).

But if I type less than 16 characters, for example:

enter image description here

Here, I typed the character a 14 times and I pressed Enter(so hexdump received 15 characters (14a + \n)). And in this case hexdump did not display anything.

Can I make hexdump display the hex values for whatever length of characters it receives instead of it waiting for 16 characters to be received?

Note: I do not want to "use options both for hexdump and xxd to display one byte as hex per line" (as suggested in a comment here). What I want to do basically is for example to know what the hex value for A without having to type an extra 15 characters to get it.

Pierre.Vriens
  • 1,088
  • 21
  • 13
  • 16
user258851
  • 129
  • 1
  • 5
  • IIRC output lines are buffered by default, not sure ATM if you can change that from the shell. But you can use options both for `hexdump` and `xxd` to display one byte as hex per line, would that be acceptable? – dirkt Nov 04 '17 at 07:56
  • Try `hexdump -v -e '/1 "%02X\n"'`. Then you only have to type `A` and return to know the hex value for `A`. You still have to type return, because the shell buffers the input line. `man ascii` also works. :-) – dirkt Nov 04 '17 at 08:55
  • Unneeded screenshots are evil, please use text copy-paste if it is possible. – peterh Feb 08 '18 at 12:06

2 Answers2

3

Try hexdump -v -e '/1 "%02X\n"'. That displays one hex byte per line, so the line output buffering won't stop the line from being displayed.

Then you only have to type A and return to know the hex value for A. You still have to type return, because the shell buffers also does line buffering on the input.

man ascii also works. :-)

dirkt
  • 31,679
  • 3
  • 40
  • 73
0

It's possible to write to same line unbuffered wrapping hexdump to stdbuf. Bash example (requires it due to some read command options):

while read -s -n1 k; do printf $k; done | stdbuf -o0 hexdump -e '1/1 "%x"'