# Array of bytes to string

**URL:** https://discourse.julialang.org/t/array-of-bytes-to-string/6793
**Category:** General Usage
**Tags:** strings
**Created:** [October 31, 2017, 6:42am UTC](https://discourse.julialang.org/t/array-of-bytes-to-string/6793 "2017-10-31T06:42:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)
#### Post date: [October 31, 2017, 6:42am UTC](https://discourse.julialang.org/t/array-of-bytes-to-string/6793/1 "2017-10-31T06:42:15Z")

</div>

Hello,  
I get from an external interface an a::Array{UInt32,1} of a fixed length (assume length(a) == 600). This is in principle a buffer. A CString is written into this buffer, last character is \0 and the rest of the array is just a junk. If I do

```julia
s = String(a) 

```

I get a string of lenght 600, although the Cstring is much shorter. Is there a function that would read the array as a Cstring?  
I know that during ccall this is done automatically, but I’m obtaining the string byte array from a HDF5 file saved by a different program and I couldn’t find the easy way. Of course I can scan the string byte by byte and than resize it but I feel there is a simple function for it already written, just don’t know its name.  
Thank you for the help.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 31, 2017, 10:31am UTC](https://discourse.julialang.org/t/array-of-bytes-to-string/6793/2 "2017-10-31T10:31:14Z")

</div>

I find it curious that `String` converts `Vector{UInt32}` at all, because

```julia
julia> a = UInt32.(['a', 'b'])
2-element Array{UInt32,1}:
 0x00000061
 0x00000062

julia> String(a)
ERROR: MethodError: Cannot `convert` an object of type Array{UInt32,1} to an object of type String
This may have arisen from a call to the constructor String(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] String(::Array{UInt32,1}) at ./sysimg.jl:77

```

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [October 31, 2017, 1:58pm UTC](https://discourse.julialang.org/t/array-of-bytes-to-string/6793/3 "2017-10-31T13:58:35Z")

</div>

I only just started playing around with IO stuff, but here’s my attempt:

```julia
# create example problem data
s = "abcdef"
a = rand(UInt32, 600)
writebuf = IOBuffer(reinterpret(UInt8, a), false, true)
write(writebuf, s)
write(writebuf, 0x00) # null-terminate

# create an IOBuffer object that wraps the raw buffer
readbuf = IOBuffer(reinterpret(UInt8, a))

# read to next null
String(readuntil(readbuf, 0x00)) # this includes the \0

```

---

<div class="post-metadata">

### Author: ![phlavenk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phlavenk/32/1873_2.png) [@phlavenk](https://discourse.julialang.org/u/phlavenk)
#### Post date: [October 31, 2017, 3:34pm UTC](https://discourse.julialang.org/t/array-of-bytes-to-string/6793/4 "2017-10-31T15:34:36Z")

</div>

My fault, it’s UInt8… My mistake.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 31, 2017, 6:10pm UTC](https://discourse.julialang.org/t/array-of-bytes-to-string/6793/5 "2017-10-31T18:10:35Z")

</div>

You should find the position of the zero (eg `findfirst`), and either copy the relevant part (eg `convert(String, a[1:zeropos])`), or return a view to it. You have to think about who manages the memory for the buffer (C? Julia?) and whether you need the string after the buffer is overwritten/freed. See also  
[https://github.com/quinnj/WeakRefStrings.jl](https://github.com/quinnj/WeakRefStrings.jl)
