DEV Community

Chris Wendt
Chris Wendt

Posted on β€’ Edited on

Haskell strings field guide

Here's how to convert between byte, string, and character types in Haskell:

Function Type
encodeUtf8 Text -> ByteString
unpack Text -> String
decodeLatin1 ByteString -> Text
decodeUtf8With lenientDecode ByteString -> Text
BC.unpack ByteString -> String
UTF8.toString ByteString -> String
B.unpack ByteString -> [Word8]
UTF8.fromString String -> ByteString
pack String -> Text
w2c Word8 -> Char
B.pack [Word8] -> ByteString
c2w ❌ partial Char -> Word8
BC.pack ❌ partial String -> ByteString
BSC.string ❌ stops streaming String -> BSC.ByteString

Note that Char is finite:

toEnum 10000000 :: Char
*** Exception: Prelude.chr: bad argument: 10000000
Enter fullscreen mode Exit fullscreen mode

Image of Stellar post

Check out Episode 1: How a Hackathon Project Became a Web3 Startup πŸš€

Ever wondered what it takes to build a web3 startup from scratch? In the Stellar Dev Diaries series, we follow the journey of a team of developers building on the Stellar Network as they go from hackathon win to getting funded and launching on mainnet.

Read more

Top comments (0)

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay