简体   繁体   中英

Windows console doesn't seem to like Unicode

I usually develop Haskell programs on Linux, and then build and test some of them on Windows. Say, I have some console program that outputs Unicode symbols. In particular, it happens to output copyright symbol '©', like this:

Copyright © 2015 Boo

It works fine on Arch Linux, but on Windows 7 it prints something like:

Copyright program_name.EXE: <stdout>: commitAndReleaseBuffer: invalid
argument (invalid character)

I'm not sure, but I think it's should be feasible to output unicode symbols in Windows console without any additional magic.

Here are two questions:

  1. Is it Windows' or Haskell's fault?
  2. How can I fix it?

PS MinGHC has been used, since it has GHC 7.10.

This function takes the current text encoding of a handle and makes it substitute safe characters like "?" when outputting characters that are not supported by the console (nb: it's the Windows console itself that doesn't support these characters, but most other languages with unicode support apply work-arounds by default)

makeSafe h = do
  e <- hGetEncoding h
  case e of
    Nothing -> return ()
    Just e1 -> do
      e' <- mkTextEncoding (show e1 ++ "//TRANSLIT")
      hSetEncoding h e'

It can be used at the beginning of the main function like this:

main = do
  mapM_ makeSafe [stdout,stderr,stdin]
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM