Read array of char from a binary file

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:

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)


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

1 Like