paperlined.org
rosetta_stone > os
document updated 12 years ago, on Aug 23, 2011

How to do a hex-dump of anything.

cat -v

That's it. That will turn non-printable characters into ^H and similar.

If Vim is available

xxd is bundled with Vim (it's actually an important part of Vim), so it's available in many places.

cat binary | xxd

od

The program "od" (octal dump) can produce hex dumps:

cat binary | od -t x2 -t c

Or just convert non-printable characters into printable ones:

cat binary | od -c

Perl script

Where Perl is available, you can use this:
###### xd -- hexdump
#!/usr/bin/perl

# do a hex-dump, similar to "xxd"

        use strict;
        use warnings;

my @fill = ("  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ", "  ");

hexdump(*STDIN) unless @ARGV;
foreach my $file (@ARGV) {
    print "File: ", $file, "\n";
    open IN, $file;
    hexdump(*IN);
    close IN;
}

sub hexdump {
    local *IN = shift;
    my ($form, $buf, $i);
    while (my $rb = read(IN, my $buf, 16)) {
        my @x = unpack("H2" x $rb, $buf);
        $buf =~ s/[\x00-\x1f\x7f-\xff]/./g;
        $buf .= ' ' x (16-length($buf));
        printf "%06x0: %s  %s\n",
            $i++,
            sprintf ("%s%s %s%s %s%s %s%s %s%s %s%s %s%s %s%s", @x, @fill),
            $buf;
    }
}