windows - 为什么管道我的命令通过 | % {echo "$_"} 让 UTF-8 工作?
问题描述
在 Windows PowerShell 中,我使用chcp 65001
并选择了一种包含所有我想要的字符的字体。
如果我显示一个 UTF-8 文件,type file.u8
它工作正常,我得到所需的字符。
如果我运行,myprogram.exe
那么在第一个非 ASCII 字符之后没有输出(如果在chcp 65001
此之前运行产生mojibake)。
如果我运行,myprogram.exe > test.u8
然后type test.u8
运行,我会得到所需的输出。
所以我推断我可以绕过该文件(使用我有限的 PowerShell 知识!),myprogram.exe | % {echo "$_"}
并且有效。因此,当 C++ 运行时直接与破坏 UTF-8 输出的控制台对话时,它似乎正在做一些特别的事情。
(如果我使用宽字符,我可以得到想要的输出,但我实际上并不想要 UTF-16 输出,我想要 UTF-8。我只想要打印调试信息的便利,而不需要额外的字符转换)
解决方案
In a comment exchange with @eryksun I realized I had overlooked an experiment: All of my attempts to use wide characters had been successful. So what if type
and echo
are actually capable of reading UTF-8 and outputting wide characters? So I redirected to a file:
myprogram.exe | % {echo "$_"} > test.txt
Now inspecting that text file it is detected as "UCS-2 LE BOM" by Notepad++. In fact, all of the cases that worked (type
, all redirection into files, etc) all produced multi-byte characters. Even type foo.u8 > foo.txt
shows the expected increase in size.
So the real issue is not my program (which is successfully outputting UTF-8) it's that there are several things capable of silently transforming that into something Windows likes.
推荐阅读
- plot - 如何在 Octave 中设置用户定义的颜色图?
- ios - 升级 SDK 后,使用 Facebook 登录在 iOS 11 上没有响应
- c# - 解析具有不同内容的 JSON 响应
- ios - 如何从 url 制作模型、视图模型
- android - 如何在 Android 中使用 SQLcipher 数据库保护 Greendao 使用的密码?
- git - 如何找到将文件更改为当前状态的 Git 提交?
- dialogflow-es - 对话流 Web 挂钩超时
- swift - 多组正则表达式 swift
- css - Odoo 和 WkHtmlToPDF:字体问题
- c# - 尝试使用c#从数据库中返回记录