简体   繁体   English

Haskell 如何将 Char 转换为 Word8

[英]Haskell How to convert Char to Word8

I want to split ByteString to words like so:我想将ByteString拆分为这样的单词:

import qualified Data.ByteString as BS

main = do
    input <- BS.getLine
    let xs = BS.split ' ' input 

But it appears that GHC can't convert a character literal to Word8 by itself, so I got:但似乎 GHC 无法自行将字符文字转换为Word8 ,所以我得到了:

Couldn't match expected type `GHC.Word.Word8'
            with actual type `Char'
In the first argument of `BS.split', namely ' '
In the expression: BS.split ' ' input

Hoogle doesn't find anything with type signature of Char -> Word8 and Word.Word8 ' ' is invalid type constructor. Hoogle 没有找到任何具有Char -> Word8类型签名的Char -> Word8并且Word.Word8 ' '是无效的类型构造函数。 Any ideas on how to fix it?关于如何解决它的任何想法?

The Data.ByteString.Char8 module allows you to treat Word8 values in the bytestrings as Char . Data.ByteString.Char8模块允许您将字节字符串中的Word8值视为Char Just 只是

import qualified Data.ByteString.Char8 as C

then refer to eg C.split . 然后参考例如C.split It's the same bytestring under the hood, but the Char -oriented functions are provided for convenient byte/ascii parsing. 它是相同的字节串,但提供了面向Char的函数,可方便地进行字节/ ASCII解析。

In case you really need Data.ByteString (not Data.ByteString.Char8), you could do what Data.ByteString itself does to convert between Word8 to Char: 如果您确实需要Data.ByteString(而不是Data.ByteString.Char8),则可以执行Data.ByteString本身所做的操作以在Word8和Char之间进行转换:

import qualified Data.ByteString as BS
import qualified Data.ByteString.Internal as BS (c2w, w2c)

main = do
    input <- BS.getLine
    let xs = BS.split (BS.c2w ' ') input 
    return ()

People looking for a simple Char -> Word8 with base library: 人们在寻找具有基本库的简单Char -> Word8

import Data.Word

charToWord8 :: Char -> Word8
charToWord8 = toEnum . fromEnum

I want to directly address the question in the subject line, which led me here in the first place. 我想直接在主题栏中解决这个问题,这首先导致了我的到来。

You can convert a single Char to a single Word8 with fromIntegral.ord : 您可以使用fromIntegral.ord将单个Char转换为单个Word8

λ> import qualified Data.ByteString as BS
λ> import Data.Char(ord)

λ> BS.split (fromIntegral.ord $ 'd') $ BS.pack . map (fromIntegral.ord) $ "abcdef"

["abc","ef"]

Keep in mind that this conversion will be prone to overflows as demonstrated below. 请记住,这种转换很容易发生溢出 ,如下所示。 You have to assure that your Char fits in 8 bits , if you do not want this to occur. 必须确保你的Char符合8位 ,如果你不希望出现这种情况。

λ> 260 :: Word8

4

Of course, for your particular problem, it is preferable to use the Data.ByteString.Char8 module as already pointed out in the accepted answer. 当然,对于您的特定问题,最好使用Data.ByteString.Char8模块,该模块已在接受的答案中指出。

Another possible solution is the following:另一种可能的解决方案如下:

charToWord8 :: Char -> Word8
charToWord8 = fromIntegral . ord
{-# INLINE charToWord8 #-}

where ord :: Chat → Int and the rest one can infer.其中ord :: Chat → Int和其他人可以推断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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