如何在Windows上打印UTF8文本到标准输出

2022-01-25 04:53:58 标签 windowsutf-8tclstdout

my little cross-platform Tcl/Tk pet-project uses utf-8 as the text encoding everywhere。

encoding system utf-8
fconfigure stderr -encoding utf-8
fconfigure stdout -encoding utf-8
#puts "汉语"
puts "foo"

This works great on Linux and macOS where the standard encoding is UTf-8:

$ tclsh utf8test.tcl
foo

Unfortunately it doesn't work at all on Windows where the terminal typically uses cp1252 and it consequently outputs garbage:

$ wine tclsh85.exe utf8test.tcl
潦?

As far as I understand this is because we cannot really change the encoding of the Windows terminal and - according to this sf issue - I need to detect myself whether the output is going to a terminal or somewhere else。

I have no idea how to make this decision。

So how can I output printable characters to the console in a project that otherwise uses UTF-8 everywhere?

edit1

My original example would output some high-Unicode points (Chinese characters Chinese)。

however the problem also persists if I try to output a single ASCII string (foo).)。

since most of the text i'm outputting is ASCII anyhow I would like at least to fix the output for that 。

i thought that cp1252 would be backwards compatible with ASCII。。。

###Output to the real Console on Windows is special -- it has it's own channel implementation -- since the low-level API takes Unicode (I think it is actually UTF-16 but that isn't important here) and Tcl uses the low-level IO API on Windows where it can。 I do not know whether Tcl has automatically detected your console correctly。 I know absolutely nothing about what interaction there is between the API and Wine's implementation of it and whatever is going on in the host environment。

Console output is supposed to Just Work? if you don't modify the encoding on its channel。 Indeed the usual rule is don't change encodings except on files and sockets (where you know the encoding might be not the system one)。

阅读全文

▼ 版权说明

相关文章也很精彩
推荐内容
更多标签
相关热门
全站排行
随便看看

错说 cuoshuo.com —— 程序员的报错记录

部分内容根据CC版权协议转载;网站内容仅供参考,生产环境使用务必查阅官方文档

辽ICP备19011660号-5

×

扫码关注公众号:职场神器
发送: 1
获取永久解锁本站全部文章的验证码