How to convert String/Text/ByteString in Haskell

Posted on January 24, 2020

Lookup table for the conversion of String/Text/ByteString in Haskell:

  - base
  - bytestring
  - text
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE

import qualified Data.Text.Lazy as TL
import qualified Data.Text.Lazy.Encoding as TEL

import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BC

import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString.Lazy.Char8 as BCL
From/To String Text strict Text lazy ByteString strict ByteString lazy
String id T.pack TL.pack BC.pack BCL.pack
Text strict T.unpack id TL.fromStrict TE.encodeUtf8 TEL.encodeUtf8 . TL.fromStrict
Text lazy TL.unpack TL.toStrict id BL.fromStrict . TEL.encodeUtf8 TEL.encodeUtf8
ByteString strict BC.unpack TE.decodeUtf8 TEL.decodeUtf8 . BL.fromStrict id BL.fromStrict
ByteString lazy BCL.unpack TL.toStrict . TEL.decodeUtf8 TEL.decodeUtf8 BL.toStrict id


import Data.ByteString.Internal (w2c, c2w)

-- w2c :: Word8 -> Char
-- c2w :: Char -> Word8

-- BS.pack :: [Word8] -> BS.ByteString
-- BS.unpack :: BS.ByteString -> [Word8]