我正在嘗試從已轉換為位元組字符的字串創建 Char8 ByteString。
例如,對于字串“65”,我想要 ByteString,解壓方法會給我“A”。因為 65 等于chr 65. 如果此方法能夠處理十六進制表示中的數字,那就更好了。所以我想要這樣的東西:
toByteString :: String -> ByteString
toByteString s = **** s
--Numbers bettwen 0 and 255 or 00 and ff
someBS = toByteString " 72 97 115 107 104 101 108 108" -- evaluates to ByteString
str :: String
str bs = unpack someBS -- evaluates to "Haskhell"
我已經嘗試在ByteString.Char8和 的檔案中找到我想要的東西Char,但是在這些庫中似乎沒有針對我的目的的內置方法。也許我錯過了一些東西。
我認為必須有一種方法可以從字符創建 ByteString 并且以某種方式找到它不夠明顯
uj5u.com熱心網友回復:
String如果你能幫上忙,不要從 開始。例如,原生 Haskell 語法已經支持十進制和十六進制的數字:
Data.ByteString> pack [72, 97, 115, 107, 104, 101, 108, 108]
"Haskhell"
Data.ByteString> pack [0x48, 0x61, 0x73, 0x6b, 0x68, 0x65, 0x6c, 0x6c]
"Haskhell"
如果您必須從 開始String,任何決議器組合器庫都可以很好地讓您從" 72 97 115"開始[72, 97, 115]- 甚至只是map read . words。
> import qualified Data.ByteString as BS
BS> BS.pack . map read . words $ " 72 97 115 107 0x68 0x65 0x6c 0x6c"
"Haskhell"
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/404605.html
標籤:
