Reading in 4 byte words from binary in Julia

I have a simple binary file containing 32 bit floats adjacent to each other.

Using Julia, I would like to read every number (i.e. every 32-bit word) and put them sequentially in an array of Float32 format.

I tried several different things, looking at the documentation , but all of them gave impossible values ​​(I use a binary file with known values ​​as a dummy input). It seems that

  • Julia reads the binary one byte at a time.

  • Julia puts every byte in 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.

Does anyone know how I should act?

+6
source share
2 answers

I found a problem. The correct way to import binary data in a single-precision floating-point format is read(f, Float32, NUM_VALS) , where f is the file stream, Float32 is the data type, and NUM_VALS is the number of words (values ​​or data points) in the binary data file .

It turns out that every time you call read(f, [...]) , the data pointer iterates over the next element in the binary.

This allows people to read data in turn simply:

 f = open("my_file.bin") first_item = read(f, Float32) second_item = read(f, Float32) # etc ... 

However, I wanted to load all the data in one line of code. When I debugged, I used read() several times in the same file pointer without re-declaring the file pointer. As a result, when I experimented with the correct operation, namely read(f, Float32, NUM_VALS) , I received an unexpected value.

+7
source

I am not sure if it is best to read it as Float32 directly, but if I set an array of 4 * n Uint8 s, I would turn it into an array of Float32 using reinterpret ( doc link ):

 raw = rand(Uint8, 4*10) # ie a vector of Uint8 aka bytes floats = reinterpret(Float32, raw) # now a vector of 10 Float32s 

With an exit:

 julia> raw = rand(Uint8, 4*2) 8-element Array{Uint8,1}: 0xc8 0xa3 0xac 0x12 0xcd 0xa2 0xd3 0x51 julia> floats = reinterpret(Float32, raw) 2-element Array{Float32,1}: 1.08951e-27 1.13621e11 
+8
source

Source: https://habr.com/ru/post/973666/


All Articles