# Read array of char from a binary file

**URL:** <https://discourse.julialang.org/t/read-array-of-char-from-a-binary-file/18906>\
**Category:** New to Julia\
**Tags:** binaryio\
**Created:** [December 21, 2018, 5:47pm UTC](https://discourse.julialang.org/t/read-array-of-char-from-a-binary-file/18906 "2018-12-21T17:47:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mgalenb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mgalenb/32/5645_2.png) [@mgalenb](https://discourse.julialang.org/u/mgalenb)\
**Post date:** [December 21, 2018, 5:47pm UTC](https://discourse.julialang.org/t/read-array-of-char-from-a-binary-file/18906/1 "2018-12-21T17:47:48Z")

</div>

I have a binary file i am reading out with julia. In the file header there is a large ‘setup’ structure with data of many different types. One of the data types is an array of char (8-bit signed integer) terminated by a 0 byte. I am given the size of the array (121). i am not sure how to implement the parsing code in julia. Here is a snippet of the code i have writen so far:

```julia
function cineheader(fname)
    h = OrderedDict()

    open(fname) do f
        # Check magic number
        read(f, UInt16) == UInt(18755) || error(basename(fname), " is not a .cine file")
        
        fhtypes = OrderedDict( # File header
            :HeadSize => UInt16,
            :Compression => UInt16,
            :Version => UInt16,
            :FirstImageIndex => Int32,
            :TotalImages => UInt32,
            :FirstImageNum => Int32,
            :ImCount => UInt32,
            :OffImageHeader => UInt32,
            :OffSetup => UInt32,
            :OffImageOffset => UInt32,
            :TriggerFrac => UInt32,
            :TriggerSec => UInt32
            :DescriptionOld => **array of char terminated by a 0 byte**
        )

        for (ID, headertype) in fhtypes # Read .cine file header
            h[ID] = read(f, headertype)
        end

end

```

---

<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 21, 2018, 6:03pm UTC](https://discourse.julialang.org/t/read-array-of-char-from-a-binary-file/18906/2 "2018-12-21T18:03:56Z")

</div>

> [@mgalenb](#):
>
> One of the data types is an array of char (8-bit signed integer) terminated by a 0 byte. I am given the size of the array (121). i am not sure how to implement the parsing code in julia.

You can do `readuntil(f, UInt8(0))` to read until a 0 byte and put the preceding bytes into a `String`. Or, if you know the number of bytes, you could do `String(read(f, numbytes))`.

(I’m assuming by “array of `char`” you mean ASCII or UTF8 data. If you have some other encoding, you can either read it into a raw array of bytes or convert it with [GitHub - JuliaStrings/StringEncodings.jl: String encoding conversion in Julia using iconv](https://github.com/JuliaStrings/StringEncodings.jl))
