Search code examples
command-linefloating-pointbinaryfilesod

How can I dump a file of float values to the command-line?


I have a file containing a raw sequence of 4-byte floating-point values - no headers, no formats, nothing else. I want to print some the float values in human-readable format, say from the n_1'th float to the n_2'th float, inclusive.

What's a simple way of doing this in a Unix-like command-line environment?


Solution

  • Use the octal dump utility, named od on Unix systems; it supports parsing floating-point binary representations as well.

    Specifically, and letting N1 = n_1 * 4, N2 = n_2 * 4 and N3 = N2 - N1, invoke:

    od -An -w4 -tf4 -j N1 -N N3 file_with_floats.bin
    

    to get something like:

            -123.456
                 inf
             111.222
    

    for a file with three 4-byte float values.

    Explanation:

    • -w4 : Print the dumped values of 4 bytes per line (i.e. one float value)
    • -An : No prefixing each line with the starting offset into the file
    • -tf4 : Floating-point format, 4 bytes per value
    • -j N1 : Skip N1 bytes
    • -N N3 : Dump N3 bytes

    If you want to print your file in C columns use 4*C as the argument to -w, e.g. -w20 will give you 5 columns per line.