简体   繁体   中英

Ptr Word8 to [Word8]

I'm using the library Network.Pcap , and it has the function toBS

toBS :: (PktHdr, Ptr Word8) -> IO (PktHdr, B.ByteString)
toBS (hdr, ptr) = do
    let len = hdrCaptureLength hdr
    s <- B.create (fromIntegral len) $ \p -> B.memcpy p ptr (fromIntegral len)
    return (hdr, s)

Right now I am unpacking the ByteString which gives me the [Word8] I want. I' m looking for a way to avoid having to unpack the ByteString , and get the [Word8] directly.

(1) Does this function exist? (2) If not, could I get some advice on how to proceed?

I'm not familiar with those libraries, but can't you just do this?

import Foreign.Marshal.Array

toByteList :: PktHdr -> Ptr Word8 -> IO [Word8]
toByteList hdr ptr = peekArray (fromIntegral (hdrCaptureLength hdr)) ptr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM