# Memcpy in julia

**URL:** https://discourse.julialang.org/t/memcpy-in-julia/67791
**Category:** Performance
**Tags:** question
**Created:** [September 7, 2021, 7:01am UTC](https://discourse.julialang.org/t/memcpy-in-julia/67791 "2021-09-07T07:01:09Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Beh123](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@Beh123](https://discourse.julialang.org/u/Beh123)
#### Post date: [September 7, 2021, 7:01am UTC](https://discourse.julialang.org/t/memcpy-in-julia/67791/1 "2021-09-07T07:01:09Z")

</div>

Hi everyone  
I have a vector which contains 5 uint8 elements. How do I copy them to two int32 variables ? we can do it with memcpy in c. Is there similar command in julia?

```julia
x=[0x3 0x00 0x11 0x00 0x80]
m= convert(Int32, signed(x[3])&0x0f)<<16 | convert( Int32, signed(x[2]))<<8 | convert( Int32, signed(x[1]));
n= convert(Int32, signed(x[5]))<<16 |convert(Int32, signed(x[4]))<<8 | convert (Int32, signed(x[3])>>4);

```

This code is run but it isn’t optimal. Is there other way? Thanks alot.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 7, 2021, 7:09am UTC](https://discourse.julialang.org/t/memcpy-in-julia/67791/2 "2021-09-07T07:09:03Z")

</div>

Use `reinterpret`.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 7, 2021, 7:29am UTC](https://discourse.julialang.org/t/memcpy-in-julia/67791/3 "2021-09-07T07:29:02Z")

</div>

I see now that you’ve edited your question (which was about 4 `UInt8` instead of 5).

If you show code, please surround it in triple backticks (```) to make it easier to read & follow, and ultimately to [make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757).

* * *

I’m not sure using `memcpy` to interpret 5 bytes as two `Int32` works in C either - you’ll interpret bytes outside of the array as part of the last `Int32`, which is a dangerous thing (and probably undefined behavior as well). Where do you get those bytes from? What do you want to do with them?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 7, 2021, 7:37am UTC](https://discourse.julialang.org/t/memcpy-in-julia/67791/4 "2021-09-07T07:37:45Z")

</div>

Assuming you want to have the top 3 bytes to be `0x00`, you can just

```julia
while length(x) % 4 != 0
  push!(x, zero(UInt8))
end

```

(or calculate the new size statically and `append!` the correct amount directly) and then do `reinterpret(Int32, x)`.

---

<div class="post-metadata">

### Author: ![Beh123](https://avatars.discourse-cdn.com/v4/letter/b/13edae/32.png) [@Beh123](https://discourse.julialang.org/u/Beh123)
#### Post date: [September 7, 2021, 8:27am UTC](https://discourse.julialang.org/t/memcpy-in-julia/67791/5 "2021-09-07T08:27:16Z")

</div>

I recieve data from udp network. Each 20 bits is a variable. How do I spilit data?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [September 7, 2021, 8:45am UTC](https://discourse.julialang.org/t/memcpy-in-julia/67791/6 "2021-09-07T08:45:57Z")

</div>

With bit shifts, something along the lines of your original post.
