# How to get an Array{UInt8,1} from Unsigned without passing by String?

**URL:** <https://discourse.julialang.org/t/how-to-get-an-array-uint8-1-from-unsigned-without-passing-by-string/18462>\
**Category:** New to Julia\
**Created:** [December 8, 2018, 2:34pm UTC](https://discourse.julialang.org/t/how-to-get-an-array-uint8-1-from-unsigned-without-passing-by-string/18462 "2018-12-08T14:34:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![roshii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roshii/32/6211_2.png) [@roshii](https://discourse.julialang.org/u/roshii)\
**Post date:** [December 8, 2018, 2:34pm UTC](https://discourse.julialang.org/t/how-to-get-an-array-uint8-1-from-unsigned-without-passing-by-string/18462/1 "2018-12-08T14:34:44Z")

</div>

I’m trying to pack UInt to Array{UInt8,1} without going through String without much success.

From byte to UInt, I would just do

```julia
T = UInt32
x = rand(UInt8,sizeof(T))
4-element Array{UInt8,1}:
 0xd6
 ...
y = bswap(reinterpret(T, x)[1])
0xdd9571d6

```

Since reinterpret has an Array as output I though it would just work the other way around but no…

```julia
reverse!(reinterpret(UInt8, y))
ERROR: bitcast: argument size does not match size of target type

```

For now I’m doing as follows but wonder if there would be something more straight forward, without going through String type?

```julia
hex2bytes(string(y, base=16))
4-element Array{UInt8,1}:
 ...

```

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [December 8, 2018, 4:26pm UTC](https://discourse.julialang.org/t/how-to-get-an-array-uint8-1-from-unsigned-without-passing-by-string/18462/2 "2018-12-08T16:26:37Z")

</div>

Does the following address your issue?

```julia
julia> T = UInt32
UInt32

julia> x = rand(UInt8,sizeof(T))
4-element Array{UInt8,1}:
 0xbe
 0x98
 0x11
 0x52

julia> y = reverse(x)
4-element Array{UInt8,1}:
 0x52
 0x11
 0x98
 0xbe

```

---

<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:** [December 8, 2018, 4:44pm UTC](https://discourse.julialang.org/t/how-to-get-an-array-uint8-1-from-unsigned-without-passing-by-string/18462/3 "2018-12-08T16:44:06Z")

</div>

Either of these seems to do what you want:

```julia
julia> reinterpret(UInt8, [bswap(y)])
4-element reinterpret(UInt8, ::Array{UInt32,1}):
 0xd6
 0x71
 0x95
 0xdd

julia> [(y>>24)%UInt8, (y>>16)%UInt8, (y>>8)%UInt8, y%UInt8]
4-element Array{UInt8,1}:
 0xd6
 0x71
 0x95
 0xdd

```

---

<div class="post-metadata">

**Author:** ![roshii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roshii/32/6211_2.png) [@roshii](https://discourse.julialang.org/u/roshii)\
**Post date:** [December 8, 2018, 5:42pm UTC](https://discourse.julialang.org/t/how-to-get-an-array-uint8-1-from-unsigned-without-passing-by-string/18462/4 "2018-12-08T17:42:28Z")

</div>

Exactly what I was looking for, thanks 🙂

---

<div class="post-metadata">

**Author:** ![roshii](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roshii/32/6211_2.png) [@roshii](https://discourse.julialang.org/u/roshii)\
**Post date:** [December 8, 2018, 5:47pm UTC](https://discourse.julialang.org/t/how-to-get-an-array-uint8-1-from-unsigned-without-passing-by-string/18462/5 "2018-12-08T17:47:00Z")

</div>

> [@jandehaan](#):
>
> y = reverse(x)

That would not work as I needed to go through the stage were `y = 0x521198be` and translate it back to x
