如何配置终端以读取UTF-8字符? [英] How do I configure a terminal to read UTF-8 characters?

查看:74
本文介绍了如何配置终端以读取UTF-8字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目上,该项目通过命令行接受用户输入.我使用的是最新的Windows 10,并且(经过大量运行……)我知道在处理UTF-8字符时这是非常糟糕的.因此,我查看了VS Code和集成终端(PowerShell)来执行程序的输入.可悲的是,终端似乎无法接受带有重音的UTF-8字符,例如ë".然后,我进行了更多研究,并为VS Code配置了settings.json,以实现UTF-8 BOM编码.尽管如此,终端仍无法读取带重音符号的字符.我确定我的程序不是问题,字体也不是问题.我已将代码简化为一种测试算法,该算法仅使用readline-sync接受输入(开发人员确认其与UTF-8兼容:

I am working on a project which accepts user input via the command line. I am using up-to-date Windows 10 and (after much running around in circles...) I am aware that it is notoriously bad when it comes to handling UTF-8 characters. Consequently, I looked to VS Code and the integrated terminal (PowerShell) to perform input into the program. Sadly, the terminal seemed unable to accept accented UTF-8 characters such as "ë". I then did more research and configured the settings.json for VS Code for UTF-8 BOM encoding. Still, the terminal failed to read accented characters. I am certain that my program is not the issue, nor is my font. I have reduced my code to a test algorithm that simply accepts input using readline-sync (which the developers confirm is compatible with UTF-8: https://github.com/anseki/readline-sync/issues/58) and "console.log"s it.

我一直在使用的测试用例是Héllo".当我在VS Code端子中输入Hëllo"时,程序将输出Héllo".当我尝试使用Windows 10的管理语言设置将所有应用转换为UTF-8编码并随后通过命令终端输入Héllo"时,它会输出"Hllo".我还尝试强制CMD将代码页65001与chcp 65001一起用于UTF-8编码,但它仍会产生"Hllo".

The test case I have been using is "Hëllo". When I input "Hëllo" into the VS Code terminal, my program outputs "H�llo". When I tried converting all of my apps to UTF-8 encoding using the administrative language settings for Windows 10 and subsequently input "Hëllo" via the command terminal, it output "Hllo". I also tried forcing CMD to use Code Page 65001 with chcp 65001 for UTF-8 encoding, but it still produced "Hllo".

这是我用于通过settings.json配置VS Code PowerShell终端的代码:

Here is the code I used to configure the VS Code PowerShell terminal via settings.json:

{
    "[powershell]": {
    "files.encoding": "utf8bom",
    "files.autoGuessEncoding": true
    }
}

这是我编写的简短代码,用于测试输入/输出以及是否成功读取ë"(不是):

And here is the brief code I wrote to test my input/output and whether the "ë" is being read successfully (which it is not):

const rlSync = require('readline-sync');

const name = rlSync.question('Enter Player 1 Username (Case Sensitive): ');
console.log(name);

如果大家都发现任何问题,请告诉我!

If y'all see any issues, please let me know!

我正在寻找任何方法来正确配置我的CLI以接受在程序中使用的重音字符.我并不是要将此问题限制为VS Code或Powershell.如果有办法使用基本的Windows 10 CMD来完成此任务,我会喜欢的.感谢您提供的任何帮助!< 3

I am looking for any way to properly configure my CLI to accept accented characters for use in my program. I do not mean to restrict this question to VS Code or Powershell. If there is a way to accomplish this with the basic Windows 10 CMD, I would love that. Thank you for any help y'all can provide! <3

推荐答案

您使用VSCode时是否有任何特殊原因?我认为您正在寻找System.Console OutputEncoding -不幸的是,我的默认编码仅适用于Hëllo",因此无法准确测试,而且我不知道这是否适用于VSCode.

Is there any particular reason you're using VSCode? I think you're looking for the System.Console InputEncoding/OutputEncoding - unfortunately my default encoding just works with "Hëllo" so couldn't accurately test, and I don't know if this works with VSCode.

尝试一下(一次一行)

# store current encoding settings
$i = [System.Console]::InputEncoding
$o = [System.Console]::OutputEncoding

# set encoding to UTF8
[System.Console]::InputEncoding = [System.Text.Encoding]::UTF8
[System.Console]::OutputEncoding = [System.Text.Encoding]::UTF8

# test
"Hëllo"

# revert (if you want. if you don't want, I would at least note the default encoding)
[System.Console]::InputEncoding =  $i
[System.Console]::OutputEncoding = $o

这篇关于如何配置终端以读取UTF-8字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆