Andrew
Andrew

Reputation: 8480

Haskell How to convert Char to Word8

I want to split ByteString to words like so:

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:

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. Any ideas on how to fix it?

Upvotes: 24

Views: 16380

Answers (5)

Jonathan Prieto-Cubides
Jonathan Prieto-Cubides

Reputation: 2847

Another possible solution is the following:

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

where ord :: Chat → Int and the rest one can infer.

Upvotes: 0

oo_miguel
oo_miguel

Reputation: 2379

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:

λ> 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.

λ> 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.

Upvotes: 3

Hussein AIT LAHCEN
Hussein AIT LAHCEN

Reputation: 51

People looking for a simple Char -> Word8 with base library:

import Data.Word

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

Upvotes: 5

Grwlf
Grwlf

Reputation: 956

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:

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 ()

Upvotes: 17

Don Stewart
Don Stewart

Reputation: 137997

The Data.ByteString.Char8 module allows you to treat Word8 values in the bytestrings as Char. Just

import qualified Data.ByteString.Char8 as C

then refer to e.g. C.split. It's the same bytestring under the hood, but the Char-oriented functions are provided for convenient byte/ascii parsing.

Upvotes: 35

Related Questions