# Change endianness in reinterpret

**URL:** https://discourse.julialang.org/t/change-endianness-in-reinterpret/80815
**Category:** General Usage
**Tags:** question
**Created:** [May 10, 2022, 2:23pm UTC](https://discourse.julialang.org/t/change-endianness-in-reinterpret/80815 "2022-05-10T14:23:35Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)
#### Post date: [May 10, 2022, 2:23pm UTC](https://discourse.julialang.org/t/change-endianness-in-reinterpret/80815/1 "2022-05-10T14:23:35Z")

</div>

Hello!

I am trying to reinterpret a `Vector` of bytes into a predefined type I have. However, primitive types are always intepreted as being little endian. Is there any way I can change this?

MWE:

```nohighlight
julia> struct P
           a::UInt32
           b::UInt8
           c::UInt8
           d::UInt16
           end

julia> struct Q
           a::UInt32
           b::UInt32
           end

julia> a = [0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08]

julia> reinterpret(P,a)
1-element reinterpret(P, ::Vector{UInt8}):
 P(0x04030201, 0x05, 0x06, 0x0807)

julia> reinterpret(Q,a)
1-element reinterpret(Q, ::Vector{UInt8}):
 Q(0x04030201, 0x08070605)

```

Can I get inner integers to be `0x01020304` and so on?

Thanks!

---

<div class="post-metadata">

### Author: ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)
#### Post date: [May 10, 2022, 2:31pm UTC](https://discourse.julialang.org/t/change-endianness-in-reinterpret/80815/2 "2022-05-10T14:31:47Z")

</div>

There might be a more elegant way, but this works:

```julia
Q((hton(getfield(only(reinterpret(Q, a)), f)) for f in fieldnames(Q))...)

```

---

<div class="post-metadata">

### Author: ![nandoconde](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nandoconde/32/19497_2.png) [@nandoconde](https://discourse.julialang.org/u/nandoconde)
#### Post date: [May 10, 2022, 2:52pm UTC](https://discourse.julialang.org/t/change-endianness-in-reinterpret/80815/3 "2022-05-10T14:52:53Z")

</div>

Yep, I had already thought of that. However, the solution gets more complicated when, instead of `a` and `b` being easy `UInt` types, they become more complex. For example, something along the lines of:

```nohighlight
julia> struct Q
           a::UInt32
           b::UInt32
           end
julia> struct P
           a::Q
           b::UInt8
           c::UInt8
           d::UInt16
           end

```

In this case, the algorithm must be recursive and, although possible, it becomes a pain in the ass

---

<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: [May 10, 2022, 3:22pm UTC](https://discourse.julialang.org/t/change-endianness-in-reinterpret/80815/4 "2022-05-10T15:22:16Z")

</div>

> [@nandoconde](#):
>
> In this case, the algorithm must be recursive and, although possible, it becomes a pain in the ass

You could automate writing a recursive `ntoh` method for your struct with a macro or similar, but unless you have a _lot_ of such structs (or a _lot_ of struct fields), it’s probably easier to just write `ntoh` methods by hand:

```julia
Base.ntoh(x::Q) = Q(ntoh(x.a), ntoh(x.b))
Base.ntoh(x::P) = P(ntoh(x.a), ntoh(x.b), ntoh(x.c), ntoh(x.d))

```

Then just do `ntoh.(reinterpret(P, bytes))`.

To define both `ntoh` and `hton`, you can use a simpler metaprogramming trick:

```julia
for f in (:ntoh, :hton)
    @eval Base.$f(x::Q) = Q($f(x.a), $f(x.b))
    @eval Base.$f(x::P) = P($f(x.a), $f(x.b), $f(x.c), $f(x.d))
end

```
