[英]Haskell How to Create a Word8?
我想編寫一個簡單的函數,它使用'\\n'
作為分隔符將ByteString
拆分為[ByteString]
。 我的嘗試:
import Data.ByteString
listize :: ByteString -> [ByteString]
listize xs = Data.ByteString.splitWith (=='\n') xs
這會引發錯誤,因為'\\n'
是Char
而不是Word8
,這正是Data.ByteString.splitWith
所期望的。
如何將這個簡單的字符轉換為ByteString
將使用的Word8
?
你可以只使用數字文字10
,但如果你想將字符轉換的文字,你可以使用fromIntegral (ord '\\n')
該fromIntegral
需要將轉換Int
是ord
返回到Word8
)。 你必須為ord
導入Data.Char
。
您還可以導入Data.ByteString.Char8
,它提供在同一ByteString
數據類型上使用Char
而不是Word8
函數。 (實際上,它有一個完全符合你想要的lines
函數。)但是,這通常不推薦,因為ByteString
不存儲Unicode代碼點(這是Char
代表的),而是存儲原始八位字節(即Word8
)。
如果您正在處理文本數據,則應考慮使用Text
而不是ByteString
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.