title: tcl: how to print UTF-8 text to stdout on Windows
body: <p>my little cross-platform Tcl/Tk pet-project uses utf-8 as the text encoding everywhere.</p>
  <pre><code>encoding system utf-8
  fconfigure stderr -encoding utf-8
  fconfigure stdout -encoding utf-8
  
  #puts &quot;汉语&quot;
  puts &quot;foo&quot;
  </code></pre>
  <p>This works great on Linux and macOS, where the standard encoding is UTf-8:</p>
  <pre class="lang-sh prettyprint-override"><code>$ tclsh utf8test.tcl
  foo
  </code></pre>
  <p>Unfortunately, it doesn't work at all on Windows, where the terminal typically uses <code>cp1252</code>, and it consequently outputs garbage:</p>
  <pre class="lang-sh prettyprint-override"><code>$ wine tclsh85.exe utf8test.tcl
  潦൯
  </code></pre>
  <p>As far as I understand, this is because we cannot really change the encoding of the Windows terminal, and - according to this <a href="https://sourceforge.net/p/tcl/bugs/5236/" rel="nofollow noreferrer">sf issue</a> - I need to detect myself whether the output is going to a terminal or somewhere else.</p>
  <p>I have no idea how to make this decision.</p>
  <p>So how can I output printable characters to the console in a project that otherwise uses UTF-8 everywhere?</p>
  <h3>edit1</h3>
  <p>my original example would output some high-unicode points (chinese characters <code>汉语</code>).
  however, the problem also persists if I try to output a single ASCII string (<code>foo</code>).
  since most of the text i'm outputting is ASCII anyhow, I would like at least to fix the output for <em>that</em>.</p>
  <p>i thought that cp1252 would be backwards compatible with ASCII...</p>