Change console encoding to UTF-8 by default #4417

miere43 opened this Issue Jun 27, 2016 · 4 comments


None yet

4 participants

miere43 commented Jun 27, 2016 edited


  • You can't print anything non-English to console because it's encoded to UTF-8 by Nim:
echo "привет"


  • Exception messages forwarded from Windows cannot be expressed too:
Error: unhandled exception: ╨Э╨╡ ╤Г╨┤╨░╨╡╤В╤Б╤П ╨╜╨░╨╣╤В╨╕ ╤Г╨║╨░╨╖╨░╨╜╨╜╤Л╨╣ ╤Д╨░╨╣╨╗.

To fix this stuff you need to manually change console encoding yourself:

when defined(windows):
  proc setConsoleOutputCP(codepage: cint): cint
    {.stdcall, dynlib: "kernel32", importc: "SetConsoleOutputCP".}
  proc getConsoleOutputCP(): cint
    {.stdcall, dynlib: "kernel32", importc: "GetConsoleOutputCP".}

  let g_originalOutputCP = getConsoleOutputCP()
  #65001 - utf-8 codepage
  discard setConsoleOutputCP(65001)

echo "привет"

when defined(windows):
  # Restore original codepage
  discard setConsoleOutputCP(g_originalOutputCP)


Error: unhandled exception: Не удается найти указанный файл.
shawnye commented Jul 8, 2016

It seems that XP is not supported.

Araq commented Jul 8, 2016

Create a PR please. Even Windows 10 still has this fucked up behaviour. Ridiculous.

nxshock commented Jul 29, 2016

I could try to create a pull request, but I'm not sure in which file we need to add the code. This should be before the start of the main procedure (set codepage to unicode), then after the completion (return previous codepage). Please suggest.

miere43 commented Jul 29, 2016 edited

@nxshock, I'll take care of it, don't worry.

@Araq Araq pushed a commit that closed this issue Jul 30, 2016
@miere43 miere43 fix #4417, update news f16140f
@Araq Araq closed this in f16140f Jul 30, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment