I have a binary file where records are separated by newlines. hexdump just dumps everything with a fixed column width. Is there a tool to hexdump this file, while honoring the newline separator? (Any other separator, like 0, would also be fine.)

  • 1
    That sounds a bit weird. I'm assuming this file is created by a Unix / Linux system, so the newline is \n = \x0a. Does the actual record data contain all other possible byte values, or is it restricted somehow? I suppose you'd like the hexdump output to be line by line, matching the binary data. Do you just want to see the hex data, or would you like output similar to the default output of either the hexdump or the hd programs?
    – PM 2Ring
    Oct 23 '14 at 12:14

This Python script will do it, sort of:

#!/usr/bin/env python3

import fileinput

for line in fileinput.input(mode='rb'):

There's one problem that I hadn't realized: the binary data may itself contain the separator character, messing up the output.

  • 1
    In what sense is that a hexdump? Maybe you meant print(line.encode('hex')) or something...
    – PM 2Ring
    Oct 23 '14 at 12:04
  • It's a hexdump because the input is read in binary mode ('rb'). Oct 23 '14 at 12:11
  • Ah, no. That's not what's normally meant by the term hexdump.
    – PM 2Ring
    Oct 23 '14 at 12:19
  • You are right. print will try to interpret as UTF-8. A lot of characters in my data then print like this \xc8\x02\x00, which at first glance looks like a hexdump. However, it's Python's way of saying that these are non-printable characters. Oct 23 '14 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.