Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to convert between [Char] and [Word8]?

I'm new to Haskell and I'm trying to use a pure SHA1 implementation in my app (Data.Digest.Pure.SHA) with a JSON library (AttoJSON).

AttoJSON uses Data.ByteString.Char8 bytestrings, SHA uses Data.ByteString.Lazy bytestrings, and some of my string literals in my app are [Char].

Haskell Prime's wiki page on Char types seems to indicate this is something still being worked out in the Haskell language/Prelude.

And this blogpost on unicode support lists a few libraries but its a couple years old.

What is the current best way to convert between these types, and what are some of the tradeoffs?

Thanks!

like image 875
cmars232 Avatar asked Jan 15 '11 21:01

cmars232


1 Answers

For conversion between Char8 and Word8 you should be able to use toEnum/fromEnum conversions, as they represent the same data.

For Char and Strings you might be able to get away with Data.ByteString.Char8.pack/unpack or some sort of combination of map, toEnum and fromEnum, but that throws out data if you're using anything other than ASCII.

For strings which could contain more than just ASCII a popular choice is UTF8 encoding. I like the utf8-string package for this:

http://hackage.haskell.org/packages/archive/utf8-string/0.3.6/doc/html/Codec-Binary-UTF8-String.html

like image 172
Antoine Latter Avatar answered Oct 19 '22 00:10

Antoine Latter