# String to byte array

**URL:** <https://discourse.julialang.org/t/string-to-byte-array/3741>\
**Category:** New to Julia\
**Created:** [May 16, 2017, 9:32am UTC](https://discourse.julialang.org/t/string-to-byte-array/3741 "2017-05-16T09:32:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)\
**Post date:** [May 16, 2017, 9:32am UTC](https://discourse.julialang.org/t/string-to-byte-array/3741/1 "2017-05-16T09:32:09Z")

</div>

What is a correct way to convert Julia-String UTF8 to byte array which can be exported as C-like string `char*` using ccall callback function ?  
pseudo-code:

```julia
jString = String("a_šČô_φ_привет_일_迪弗尺")
strByteArray = toByteArray(jString)
len = length(strByteArray)
# fnc is pointer to C callback function void(__stdcall*)(const char* str, const size_t len);
fnc != Ptr{Void}(0) && ccall(fnc, Void, (Cstring, Int64), strByteArray, len) 

```

---

<div class="post-metadata">

**Author:** ![traktofon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/traktofon/32/591_2.png) [@traktofon](https://discourse.julialang.org/u/traktofon)\
**Post date:** [May 16, 2017, 9:51am UTC](https://discourse.julialang.org/t/string-to-byte-array/3741/2 "2017-05-16T09:51:12Z")

</div>

You can get the UTF8 bytes as an array with `convert(Vector{UInt8}, jString)` but I guess you still need to append a zero byte for C compatibility.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [May 16, 2017, 12:24pm UTC](https://discourse.julialang.org/t/string-to-byte-array/3741/3 "2017-05-16T12:24:03Z")

</div>

No need to call something like `toByteArray`, just pass the string to `ccall`, which will handle everything automatically. The only thing you need to figure out is the length of the string in bytes, which can be obtained using `sizeof`. Note that since you pass the length as an argument, you can use `Ptr{UInt8}` instead of `Cstring` to avoid checking for the presence of null bytes in the string. See [the manual](https://docs.julialang.org/en/stable/manual/calling-c-and-fortran-code/) for details.

This would probably deserve a short mention in the manual. You could open an issue about that.

---

<div class="post-metadata">

**Author:** ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)\
**Post date:** [May 16, 2017, 2:06pm UTC](https://discourse.julialang.org/t/string-to-byte-array/3741/4 "2017-05-16T14:06:23Z")

</div>

thaks for hints. the resulting implementation:  
Julia side

```julia
  jString = String("a_šČô_φ_привет_일_迪弗尺")
  fnc != C_NULL &&
    ccall(fnc, Void, (Ptr{UInt8}, Cint), jString, sizeof(jString)) # @nalimilan nalimilan

```

CPP side: possible implementation - write data as arrived…

```julia
void callbackfn(const char* data, const int len )
{
  std::ofstream ofs("myFile.txt", std::ios::binary);
  ofs.write(data, len); // don't worry about null-terminated string
  ofs.close();
}

```

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [May 17, 2017, 1:42pm UTC](https://discourse.julialang.org/t/string-to-byte-array/3741/5 "2017-05-17T13:42:34Z")

</div>

Cool. Note you don’t even need to call `String`. Also, more importantly, use `Cint` instead of `Int64` if you define `len` as `int` in the C code.

---

<div class="post-metadata">

**Author:** ![igor.cerovsky](https://avatars.discourse-cdn.com/v4/letter/i/c37758/32.png) [@igor.cerovsky](https://discourse.julialang.org/u/igor.cerovsky)\
**Post date:** [May 17, 2017, 2:29pm UTC](https://discourse.julialang.org/t/string-to-byte-array/3741/6 "2017-05-17T14:29:05Z")

</div>

I’ve corrected the `Cint` as well ac `C_NULL` in solution the code above.
