How do I configure a terminal to read UTF-8 characters?

安稳与你 提交于 2021-02-11 14:26:32

问题


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.

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".

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!

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


回答1:


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.

Try this (one line at a time)

# 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


来源:https://stackoverflow.com/questions/57402483/how-do-i-configure-a-terminal-to-read-utf-8-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!