# Reinterpret byte to Float in julia

**URL:** https://discourse.julialang.org/t/reinterpret-byte-to-float-in-julia/111670
**Category:** Data
**Tags:** binaryio, float, io
**Created:** [March 15, 2024, 4:41pm UTC](https://discourse.julialang.org/t/reinterpret-byte-to-float-in-julia/111670 "2024-03-15T16:41:37Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [March 15, 2024, 4:41pm UTC](https://discourse.julialang.org/t/reinterpret-byte-to-float-in-julia/111670/1 "2024-03-15T16:41:37Z")

</div>

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

```julia
typeof(data) = Vector{UInt8}

```

My data looks like 👉

```julia
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 👇

 ![image](https://global.discourse-cdn.com/julialang/original/3X/b/c/bccb58f923027e11ec7835b874fe1d52862d2463.png)

---

<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: [March 15, 2024, 4:48pm UTC](https://discourse.julialang.org/t/reinterpret-byte-to-float-in-julia/111670/2 "2024-03-15T16:48:20Z")

</div>

> [@raman\_kumar](#):
>
> `read(f, N_points * 9)`

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)`?

---

<div class="post-metadata">

### Author: ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)
#### Post date: [March 15, 2024, 4:50pm UTC](https://discourse.julialang.org/t/reinterpret-byte-to-float-in-julia/111670/3 "2024-03-15T16:50:24Z")

</div>

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.

---

<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: [March 15, 2024, 4:51pm UTC](https://discourse.julialang.org/t/reinterpret-byte-to-float-in-julia/111670/4 "2024-03-15T16:51:59Z")

</div>

Note also that it is probably easier to use [`read!`](https://docs.julialang.org/en/v1/base/io-network/#Base.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:

```julia
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

```

---

<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: [March 15, 2024, 4:54pm UTC](https://discourse.julialang.org/t/reinterpret-byte-to-float-in-julia/111670/5 "2024-03-15T16:54:21Z")

</div>

> [@raman\_kumar](#):
>
> I have to read `N_points*9` amount of data from file.

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).
