Okay, eventually it boils down to this: import Data.Text import Data.Text.Encoding (encodeUtf32LE) import Data.ByteString.Unsafe (unsafeUseAsCString) textAsPtrW32 :: Text -> (Ptr Word32 -> IO a) -> IO a textAsPtrW32 t = unsafeUseAsCString (encodeUtf32LE $ t `snoc` '\0') . (. castPtr) As the function passed copies or at least does not store the pointer, I can use unsafeUseAsCString, but then I have to manually append the null-termination. Le 21 mars 2012 13:09, Antoine Latter <aslatter@gmail.com> a écrit :
On Wed, Mar 21, 2012 at 3:35 AM, Yves Parès <yves.pares@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