How can I get Ptr byte string?

Is there a way to extract the main memory pointer from a ByteString object? My current approach is wrong, the compiler says.

getPtr :: ByteString -> Ptr Word8
getPtr (PS ptr _ _) = ptr
+4
source share
1 answer

Use unsafeUseAsCStringout Data.ByteString.Unsafe. It has type:

ByteString -> (CString -> IO a) -> IO a

So you can use unsafeUseAsCString bs returnto just get the pointer. It will not end in zero if it does not ByteString, and it will have a type CString(that is Ptr CChar) instead Ptr Word8, but you can use castPtrfrom Foreign.Ptrto get around if this is a problem.

+7
source

All Articles