In my Haskell Program I need to work with Strings and ByteStrings:
import Data.ByteString.Lazy as BS (ByteString)
import Data.ByteString.Char8 as C8 (pack)
import Data.Char (chr)
stringToBS :: String -> ByteString
stringToBS str = C8.pack str
bsToString :: BS.ByteString -> String
bsToString bs = map (chr . fromEnum) . BS.unpack $ bs
bsToString
works fine, but stringToBS
results with following error at compiling:
Couldn't match expected type ‘ByteString’
with actual type ‘Data.ByteString.Internal.ByteString’
NB: ‘ByteString’ is defined in ‘Data.ByteString.Lazy.Internal’
‘Data.ByteString.Internal.ByteString’
is defined in ‘Data.ByteString.Internal’
In the expression: pack str
In an equation for ‘stringToBS’: stringToBS str = pack str
But I need to let it be ByteString
from Data.ByteString.Lazy as BS (ByteString)
for further working functions in my code.
Any idea how to solve my problem?
ByteString
is a sequence of bytes, and not a string: conversion between them fundamentally depends on a given encoding. In particular,Char8
assumes that everything is encoded using latin-1, and silently truncates everything else. – chiString
and notText
the conversion with encoding/decoding is a bit more straightforward. – epsilonhalbe