There are lots of ways of storing numbers - ASCII (which can have locale specific variants, such as using ',' to separate fractional part OR as a thousands grouping), binary integer (variable number of bits)/float/double (all of which may vary depending on endian architecture and whether software producing the file formalises the representation), BCD (uncompressed, packed, fixed point and other variants), Bi-quinary coded decimal ...
There is no standard.
This appears to be a bug in that implementation of hexdump
.
The function responsible is
print(struct hexdump_pr *pr, unsigned char *bp)
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.
case F_INT:
{
short sval; /* int16_t */
int ival; /* int32_t */
long long Lval; /* int64_t, int64_t */
switch(pr->bcnt) {
case 1:
printf(pr->fmt, (unsigned long long) *bp);
break;
case 2:
memmove(&sval, bp, sizeof(sval));
printf(pr->fmt, (unsigned long long) sval);
break;
To fix it, patch the function to use the same "copy to local variable" approach:
--- text-utils/hexdump-display.c.orig 2015-10-05 15:18:43.458759033 +0100
+++ text-utils/hexdump-display.c 2016-08-19 14:58:35.507705329 +0100
@@ -146,13 +146,15 @@
}
case F_INT:
{
+ char cval; /* int8_t */
short sval; /* int16_t */
int ival; /* int32_t */
long long Lval; /* int64_t, int64_t */
switch(pr->bcnt) {
case 1:
- printf(pr->fmt, (unsigned long long) *bp);
+ memmove(&cval, bp, sizeof(cval));
+ printf(pr->fmt, (unsigned long long) cval);
break;
case 2:
memmove(&sval, bp, sizeof(sval));
Best Answer
od
is a standard command. Inod -An -tx1 -w1 -v
, the only thing that is not POSIX is the-w
option.You can replace it with:
Which would work in any POSIX-compliant system and does work on FreeBSD at least:
Or with one
sed
invocation to replace thetr
+grep
:(or
)
With
perl
(not a POSIX command but ubiquitous on anything but embedded systems):Which is also the fastest of the 3 in my tests (compared to GNU
od
) by a significant margin for anything but small files (also thanhexdump
, not thanvim
'sxxd
).