I have a simple binary file that contains 32-bit floats adjacent to each other.
Using Julia, I would like to read each number (i.e. each 32-bit word) and put them each sequentially into a array of Float32
format.
I've tried a few different things through looking at the documentation, but all have yielded impossible values (I am using a binary file with known values as dummy input). It appears that:
Julia is reading the binary file one-byte at a time.
Julia is putting each byte into a Uint8
array.
For example, readbytes(f, 4)
gives a 4-element array of unsigned 8-bit integers. read(f, Float32, DIM)
also gives strange values.
Anyone have any idea how I should proceed?
Julia Language has changed a lot since 5 years ago. read()
no longer has API to specify Type and length simultaneously. reinterpret()
creates a view of a binary array instead of array with desired type. It seems that now the best way to do this is to pre-allocate the desired array and fill it with read!
:
data = Array{Float32, 1}(undef, 128)
read!(io, data)
This fills data
with desired float numbers.