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:

    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.