# Reading binary file to Julia

**URL:** <https://discourse.julialang.org/t/reading-binary-file-to-julia/103151>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 24, 2023, 12:23pm UTC](https://discourse.julialang.org/t/reading-binary-file-to-julia/103151 "2023-08-24T12:23:01Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![jmair](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jmair/32/35117_2.png) [@jmair](https://discourse.julialang.org/u/jmair)\
**Post date:** [August 24, 2023, 1:02pm UTC](https://discourse.julialang.org/t/reading-binary-file-to-julia/103151/2 "2023-08-24T13:02:21Z")

</div>

I don’t have some test data to be certain, but perhaps this could work:

```julia
struct Data{T}
    x::T
    y::T
    z::T
    pdf::T
end

function read_file(path; coordinate_conversion=nothing)
    data = Data{Float32}[]
    open(path, "r") do io
        
        # buffer of raw bytes for each item of data
        buffer = Vector{UInt8}(undef, sizeof(Data{Float32}))
        while !eof(io)
            readbytes!(io, buffer)
            push!(data, (reinterpret(Data{Float32}, buffer)[1]))
        end
    end
    data = @views data[5:end]
    if !isnothing(coordinate_conversion)
        data .= coordinate_conversion.(data) # convert in-place
    end

    return data
end

```

I believe `reinterpret` is little endian for the most part (see [this post](https://discourse.julialang.org/t/endian-of-reinterpret-t-array/5272)).

This isn’t the most performant implementation as it keeps resizing the array, but it should get you part of the way there.

---

_[View the full topic](https://discourse.julialang.org/t/reading-binary-file-to-julia/103151)._
