Reinterpret byte to Float in julia

I have data of UInt8 type and i want to convert it into Float64.

typeof(data) = Vector{UInt8}

My data looks like :point_right:

data = read(f, N_points * 9) = UInt8[0xdf, 0xfd, 0x5a, 0x80, 0x1c, 0x68, 0x86, 0x3f, 0xb6, 0x1a, 0x0f, 0x3a, 0x2f, 0x4e, 0x85, 0x3f, 0x3c, 0x0c, 0x29, 0xb2, 0x83, 0xe3, 0x85, 0x3f, 0xd2, 0x8d, 0xb5, 0x62, 0xc7, 0x61, 0x88, 0x3f, 0x1c, 0x00, 0x24, 0x53, 0xd3, 0x26, 0x90, 0x3f, 0x94, 0x92, 0x25, 0x6a, 0x2e, 0x50, 0x8c, 0x3f, 0x1c, 0x90, 0xd6,

I am getting error as shown below :point_down:

Why * 9? Float64 values are 8 bytes. Remember that the second argument to read is the number of bytes — did you forget to multiply by sizeof(Float64)?

1 Like

I have to read N_points*9 amount of data from file. When i remove N_points*9 from code then it is reinterpreting correctly.

Note also that it is probably easier to use read! for this sort of thing. Allocate an uninitialized Float64 array of the desired size, then read! into it — that way no reinterpret is required, and the sizeof(Float64) factor in the number of bytes to read is included automatically.

For example:

a = Array{Float64}(undef, m, n) # allocate uninitialized m×n array of Float64
read!(f, a) # read from f into a, in column-major order

If that is the number of Float64 values, then if you are calling read then you would multiply by sizeof(Float64) (the number of bytes per value) to get the number of bytes.

Anyway, I would suggest just calling read! into a 9 × N_points or N_points × 9 or N_points * 9 array of Float64 instead, as I suggested above (depending on how your data is ordered).