I want to read /dev/input/mice with hexdump. Format is described at here at StackOverflow. What I need is to read one byte (flags), print it in hex format, then read two bytes (x and y move offset) and print each in signed decimal format.

I use hexdump from util-linux 2.28 on Arch Linux.

The best I was able to put together is

hexdump /dev/input/mice -e '/1 "%03x" 2/1 " %03d" /0 "\n"'

The format string:

/1 "%03x" reads one byte and prints it as hex number

2/1 " %03d" reads one byte and prints it as signed decimal integer, repeats two times

/0 "\n" reads zero bytes and prints end of line

The problem is that for value 0xFF the %d specifier prints 255 instead of -1. The format is taken from C, which extends the width properly, so how to force this proper width extension here?

On Debian Jessie, it returns -1; on CentOS it returns 255. On CentOS hexdump comes from util-linux. On Debian it comes from bsdmainutils. There seems to be an incompatibility between the two versions!
– Stephen HarrisAug 19 '16 at 14:04

1 Answer
1

in hexdump-display.c. It copies the correct number of bytes into a suitable variable, so that sign-extending can happen when passing the value to printf(). But for single bytes, it doesn't bother - it just dereferences the pointer, which was to an unsigned char.