# Reading binary data from a file

**URL:** <https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695>\
**Category:** General Usage\
**Tags:** question, binaryio\
**Created:** [January 11, 2024, 5:16pm UTC](https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695 "2024-01-11T17:16:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![MrBean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrbean/32/206151_2.png) [@MrBean](https://discourse.julialang.org/u/MrBean)\
**Post date:** [January 11, 2024, 5:16pm UTC](https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695/1 "2024-01-11T17:16:45Z")

</div>

Dear all,  
This is my first post and I am a baby user of Julia.

I would like to read data from a file saved in **binary format** that comes from the sampler of an analytical instrument.

The data is of a complex type and acquired in phase quadrature, that is, each sample element consists of real and imaginary parts (so that two points are needed to correctly describe each data).

The data format can be **Int** or **float** type depending on the sensor connected.

The number of totally acquired complex points is known.

What I would like to do as a first step is to load two separate vectors one containing real data, the other with imaginary data, and possibly to print the value of some of that points.

But nothing… I am not capable of it! 😔

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 11, 2024, 5:24pm UTC](https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695/2 "2024-01-11T17:24:31Z")

</div>

See, for example:

- [How to read binary file in Julia? - Stack Overflow](https://stackoverflow.com/questions/59991708/how-to-read-binary-file-in-julia)
- [How do I read a binary file back into an Array?](https://discourse.julialang.org/t/how-do-i-read-a-binary-file-back-into-an-array/28454)
- [Read binary data of arbitrary dims and type](https://discourse.julialang.org/t/read-binary-data-of-arbitrary-dims-and-type/28560)

All of these appeared in the first page of Google results for “read binary file Julia”, and they all point to the same function: [`read!`](https://docs.julialang.org/en/v1/base/io-network/#Base.read!).

(Of course, this assumes that your “binary data” just consists of raw arrays written directly to a file, as opposed to a more structured binary format like HDF5.jl.)

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [January 11, 2024, 7:51pm UTC](https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695/3 "2024-01-11T19:51:03Z")

</div>

Make sure that you understand the format of the file. It would be unusual if the data acquisition of the analytical instrument wrote the binary data directly from the start of the file without any further information or header. If you want to skip initial extra information, then `seek(io, pos)` does it. `Int` could be 1, 2, 4, or 8 bytes, in the worst case something like 12 bits + a sign bit directly from the ADC (we have like this in satellite telemetry). `Float` could be 2 bytes or 4 byte single precision or 8 byte double precision, or also from some non-IEEE 754 compatible hardware.

Julia has `ComplexF16`, `ComplexF32` ,`ComplexF64`, and `Complex{Intxx}` types. Using those is a bit easier and slightly more performand than reading into separate real and imaginary arrays (if the file has always imaginary following the real part).

Edit: If the byte order is different from little endian, that your computer most likely has, then `ntoh` is useful.

---

<div class="post-metadata">

**Author:** ![mike.ingold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike.ingold/32/203749_2.png) [@mike.ingold](https://discourse.julialang.org/u/mike.ingold)\
**Post date:** [January 11, 2024, 8:53pm UTC](https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695/4 "2024-01-11T20:53:25Z")

</div>

In terms of file formats, “binary data” can mean basically anything. Is the data saved in a particular standard or file extension?

---

<div class="post-metadata">

**Author:** ![MrBean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrbean/32/206151_2.png) [@MrBean](https://discourse.julialang.org/u/MrBean)\
**Post date:** [January 11, 2024, 8:55pm UTC](https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695/5 "2024-01-11T20:55:48Z")

</div>

Thank you for your reply.  
Of courses I had a look around before posting here, but unfortunately there is no examples useful for my needs.

---

<div class="post-metadata">

**Author:** ![MrBean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrbean/32/206151_2.png) [@MrBean](https://discourse.julialang.org/u/MrBean)\
**Post date:** [January 11, 2024, 8:59pm UTC](https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695/6 "2024-01-11T20:59:06Z")

</div>

Thank you Stephancb,

Let start with Int32 format, I can extend the idea to Floats.

Can you post the code for reading N complex sampled and save real and imaginary parts in two vectors?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 11, 2024, 9:16pm UTC](https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695/8 "2024-01-11T21:16:30Z")

</div>

> [@MrBean](#):
>
> Of courses I had a look around before posting here, but unfortunately there is no examples useful for my needs.

It’s hard to help you if you don’t make more of an effort — why don’t the posted links help? Have you tried using the `read!` function? Do you understand the specific format of your file?

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

---

<div class="post-metadata">

**Author:** ![stephancb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stephancb/32/14243_2.png) [@stephancb](https://discourse.julialang.org/u/stephancb)\
**Post date:** [January 11, 2024, 10:01pm UTC](https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695/9 "2024-01-11T22:01:12Z")

</div>

Type directly into the REPL or into an editor and save as e.g. `read_complexi32.jl`

```julia
function read_complexi32(filename, N)
    # Preallocate arrays:
    data_r = Vector{Int32}(undef, N)
    data_i = Vector{Int32}(undef, N)

    # Read the data into arrays
    open(filename, "r") do io
        for k in 1:N
            data_r[k] = read(io, Int32)
            data_i[k] = read(io, Int32)
        end
    end
    (real=data_r, imag=data_i)
end

```

If in a file

```julia
include("read_complexi32.jl")

```

My test in the REPL:

```julia

julia> N=1024
1024

julia> data_c = rand(Complex{Int32}, N);

julia> write("test_binary_int32", data_c)
8192

julia> z=read_complexi32("test_binary_int32", N);

julia> z.real==real(data_c)
true

julia> z.imag==imag(data_c)
true

```

There are of course variations how this can be coded. Remarks:

1. Make the reading a function, for reuse and better performance. The test is directly in the REPL

2. The function returns the two arrays as a “named tuple” with `z.real` the real array and `z.imag` the imaginary one.

3. Writing the test complex array into the file is much shorter and without explicit loop. Reading could be similarly concise, if you were persuaded to accept reading into a complex array instead of separate real and imaginary.

---

<div class="post-metadata">

**Author:** ![MrBean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrbean/32/206151_2.png) [@MrBean](https://discourse.julialang.org/u/MrBean)\
**Post date:** [January 12, 2024, 6:35am UTC](https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695/10 "2024-01-12T06:35:50Z")

</div>

Great,  
as soon as I can access to the workstation I will try the code.

---

<div class="post-metadata">

**Author:** ![MrBean](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrbean/32/206151_2.png) [@MrBean](https://discourse.julialang.org/u/MrBean)\
**Post date:** [January 12, 2024, 12:13pm UTC](https://discourse.julialang.org/t/reading-binary-data-from-a-file/108695/11 "2024-01-12T12:13:17Z")

</div>

Thanks Stephancb,  
the code works very well!
