On Sunday, June 5, 2016 at 11:41:34 PM UTC, J Luis wrote:
>
> Hi,
>
> I have one of those types generated from a C struct with Clang.jl that 
> turns a stack variable into a loooong list of members (for example (but I 
> have longer ones))
>
> https://github.com/joa-quim/GMT.jl/blob/master/src/libgmt_h.jl#L1246
>
> (an in interlude: isn't yet any better way of representing a C "char 
> str[256];"?)
>
> when executed I get (example)
>
> julia> hdr.x_units
> GMT.Array_80_Uint8(0x6c,0x6f,0x6e,0x67,0x69,0x74,0x75,0x64,0x65,0x20,0x5b,
> 0x64,0x65,0x67,0x72,0x65,0x65,0x73,0x5f,0x65,0x61,0x73,0x74,0x5d,0x00,0x00
> ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
> 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
> ,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
> 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00)
>
> but I need to transform it into a string. After some suffering I came out 
> with this solution
>
> julia> join([Char(hdr.x_units.(n)) for n=1:sizeof(hdr.x_units)])
> "longitude 
> [degrees_east]\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
>
> well, it works
>

Well, maybe not!

If you can have multi-byte string encoding, such as UTF8, then indexing by 
bytes will not work (UTF16, is not likely in your case, as \0 can happen in 
the middle of strings, other multi-byte encodings, I know less about, might 
or might not be safe). UTF8 and all the single-byte encodings I know, such 
as ASCII, strictly allow \0 in the middle of a string, while a Cstring of 
them assumes it can't happen.

There should be a function (maybe there is), where you can point to a 
Cstring (note it IS a type in Julia, for the C API.. at least) [or you 
could hack Cstring.data to point to the first byte..] Note, it might assume 
the string ends with \0, but I'm not sure in your case it will happen for 
sure.. Then you need to guard with a function that takes care of finding 
the first \0 with a fallback to max-size=x_units.

Why is:

immutable Cstring <: Any

not a subtype of AbstractString? Anyway, you may want to convert the string 
then to UTF8String.


but it's kind or ugly. Is there a better way of achieving this? Namely, how 
> could I avoid creating a string with all of those \0's? I know I can remove 
> them after, but what about not copying them on first place?
>
> Thanks
>
>

Reply via email to