[Haskell-cafe] Quickest way to pass Text to C code

Antoine Latter aslatter at gmail.com
Wed Mar 21 13:09:23 CET 2012


On Wed, Mar 21, 2012 at 3:35 AM, Yves Parès <yves.pares at gmail.com> wrote:
> Hello,
>
> I have to interact with a C++ library that accepts as string types (putting
> c++ strings aside) pointers of wchar_t (CWString in Haskell) or unsigned
> 32-bit int (Ptr Word32 for UTF-32 codepoints).
>
> I have read what text, bytestring and base provide, but Text can only be
> directly converted to (Ptr Word16), and if I use encodeUTF32 to get a
> ByteString, then I only get useAsCString, no direct conversion to CWString
> or Ptr WordXX is possible.

A CString is a (Ptr CChar). You can then use castPtr to get whichever
pointer type you need, if you believe the underlying buffer has the
representation you want (in this case, UTF-32).

It still won't be null-terminated, however.

Antoine



More information about the Haskell-Cafe mailing list