I’d like to read in a string from console which contains special characters like ö,ä,ü,µ… I’ve tried:
do …
ts <- getLine
…
but this doesn’t work for those character. For example, unicode for ö is \246, but if I use getLine to read in ö haskell reads in “\195\182”, and putStr “\195\182” gives me ö, which is not ö. What’s the problem here? Do I need another function to read in those characters?
I am using WinGHCi 7.0.3 on windows xp. I’d be glad if someone could help me because I didn’t find anything so far.
@Judah Jacobson:
I tried it again, before typing any other commands, and got this:
Prelude> :m +System.IO
Prelude System.IO> hSetEncoding stdin utf8
Prelude System.IO> getLine
ασδφ
"\206\177\207\402\206\180\207\8224"
Prelude System.IO> putStr "\206\177\207\402\206\180\207\8224"
ασδφPrelude System.IO>
I also tried the windows command chcp 65001 but it didn’t change anything, I had utf8 already activated in windows.
You need to set the encoding of stdin to UTF8. For me, this is set to CP437 initially in GHCi on Windows XP, and to UTF8 on Mac.
Check with
hGetEncoding stdin(System.IO), and set withhSetEncoding stdin utf8and it should work.Edit: This is what it looks like on my Mac: