Converting [Word8] to CString

Marcin 'Qrczak' Kowalczyk
15 Sep 2001 09:15:32 GMT

Fri, 14 Sep 2001 23:40:42 -0700, Ashley Yakeley <> pisze:

> I'm looking for a function that will convert a [Word8] byte-array
> to a CString (i.e. a C byte array) for the purposes of FFI.

You can use newArray which allocates the C byte array using malloc
so it must be freed by free, and cast the resulting pointer from
Ptr Word8 to CString using castPtr.

When the array is temporary, it's better (and faster) to use

    withArray list_of_Word8 $ \word8Ptr ->
        let cstring = castPtr word8Ptr in

> And it has to be the _same_ list of bytes that comes through on
> the native side.

I don't unserstand this.

 __("<  Marcin Kowalczyk *
  ^^                      SYGNATURA ZASTĘPCZA