在 Windows 批处理中回显 UTF-8 字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11962172/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 09:48:39  来源:igfitidea点击:

Echo UTF-8 characters in windows batch

windowsbatch-file

提问by BearCode

Can I use echo to generate an UTF-8 text file? For example if I want to generate a file that contains the character "?"

我可以使用 echo 生成 UTF-8 文本文件吗?例如,如果我想生成一个包含字符“?”的文件。

echo "abcd ?" > out.txt

(the batch file is encoded with UTF-8)

(批处理文件使用 UTF-8 编码)

the result is an ANSI encoded file and the "?" character is transformed into "ê". How can I convince "echo" to generate an UTF-8 file?

结果是一个 ANSI 编码的文件和“?” 字符转换为“ê”。如何说服“echo”生成 UTF-8 文件?

If it's not possible, then can I change the encoding of the text file after creating it? Is there any tool in the gnuwin32 package that can help me to change the encoding?

如果不可能,那么我可以在创建文本文件后更改其编码吗?gnuwin32 包中是否有任何工具可以帮助我更改编码?

thanks

谢谢

采纳答案by BearCode

The problem was that the file contained the line:

问题是该文件包含以下行:

<META content="text/html; charset=iso-8859-2" http-equiv=Content-Type> 

and then Notepad2 and Firefox was changing the charset, showing ? instead of ?. In plain Notepad, the file looks ok. The solution was to add the UTF-8 signature (Byte Order Mark) at the beginning of the file:

然后 Notepad2 和 Firefox 正在更改字符集,显示 ? 代替 ?。在普通的记事本中,文件看起来不错。解决办法是在文件开头添加UTF-8签名(字节顺序标记):

echo1 -ne \xEF\xBB\xBF > out.htm

(echo1 is from gnuwin32)

(echo1 来自 gnuwin32)

thanks for the answers

谢谢你的回答

回答by cuixiping

Use chcpcommand to change active code page to 65001 for utf-8.

使用chcp命令将 utf-8 的活动代码页更改为 65001。

chcp 65001

回答by Garrett

Try starting CMD.exe with the /U switch: it causes all pipe output to be Unicode instead of ANSI.

尝试使用 /U 开关启动 CMD.exe:它会导致所有管道输出为 Unicode 而不是 ANSI。

回答by bcag2

chcp 65001

CHCP 65001

as mention by @cuixiping is a good answer but it require to change cmd default font to Lucida?Consolefor example, as you can read here: https://superuser.com/questions/237081/whats-the-code-page-of-utf-8#272184

正如@cuixiping 所提到的,这是一个很好的答案,但它需要将 cmd 默认字体更改为Lucida?Console,例如,您可以在这里阅读:https: //superuser.com/questions/237081/whats-the-code-page- of-utf-8#272184

and of course, as mentioned by @BearCode, the text should be in utf-8… in my case, with Vim under GNU/Linux with remote access, but notepad++ is right way too!

当然,正如@BearCode 所提到的,文本应该是 utf-8 ……在我的情况下,在 GNU/Linux 下使用 Vim 可以远程访问,但是 notepad++ 也是正确的方式!

回答by whiterabbit

Appears as well as changing the code page you need to write at least one unicode character in your first echo out to the file for the file to be saved as unicode. So your batch file itself needs to be stored in a unicode format like UTF-8.

出现以及更改代码页时,您需要在第一个回显中写入至少一个 unicode 字符到文件中,以便将文件保存为 unicode。因此,您的批处理文件本身需要以 UTF-8 等 unicode 格式存储。