gpt4 book ai didi

haskell - Ptr Word8 到 ByteString

转载 作者:行者123 更新时间:2023-12-02 19:06:54 25 4
gpt4 key购买 nike

我有一个 FFI 调用返回一些数据字节(不是 CString)。目前,我正在使用如下内容:

import qualified Data.ByteString as BS

BS.pack <$> mapM (peekElem ptr) [0 .. n - 1]

是否有更有效的方法?谷歌搜索似乎指向使用 Data.ByteString.Internal,但这似乎不受欢迎(绑定(bind)到内部接口(interface))。事实上they don't seem不再为内部模块提供文档。 是否有更有效的可移植方法?在处理来自 FFI 的字节时,我经常遇到这个问题。我只想:

ptrToBs :: Ptr Word8 -> Int -> IO ByteString
ptrToBs buf n = ... -- totally fine if it makes a copy of the buffer

(我首先检查了 Hoogle。)

也许我使用了错误的类型“Binary.Get 或稍后的其他解码包消耗的原始字节”?

最佳答案

看来你想避免 CString 因为(在我看来很好)原因它们是空终止的。更有原则的CStringLen没有这个弱点,所以packCStringLen :: CStringLen -> IO ByteString应该适合您的任务。

关于haskell - Ptr Word8 到 ByteString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36023500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com