简体   繁体   English

echo Windows批次中的UTF-8个字符

[英]Echo UTF-8 characters in Windows batch

Can I use echo to generate an UTF-8 text file?我可以使用 echo 生成 UTF-8 文本文件吗? For example if I want to generate a file that contains the character ę :例如,如果我想生成一个包含字符ę的文件:

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 ê .结果是一个 ANSI 编码的文件,并且ę字符被转换为ê How can I convince echo to generate an UTF-8 file?如何说服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 package 中是否有任何工具可以帮助我更改编码?

thanks谢谢

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

chcp 65001

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

chcp 65001 CHCP 65001

as mention by @cuixiping is a good answer but it require to change cmd default font to Lucida Console for example, as you can read here: https://superuser.com/questions/237081/whats-the-code-page-of-utf-8#272184正如@cuixiping 所提到的,这是一个很好的答案,但它需要将 cmd 默认字体更改为Lucida 控制台,例如,您可以在这里阅读: 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++ 也是正确的方式!

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 ę.然后 Notepad2 和 Firefox 正在更改字符集,显示 Ä 而不是 ę。 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:解决办法是在文件开头添加UTF-8签名(字节顺序标记):

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

(echo1 is from gnuwin32) (echo1 来自 gnuwin32)

thanks for the answers谢谢你的回答

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.出现以及更改代码页时,您需要在第一个回显中写入至少一个 unicode 字符到文件中,以便将文件保存为 unicode。 So your batch file itself needs to be stored in a unicode format like UTF-8.因此,您的批处理文件本身需要以 UTF-8 等 unicode 格式存储。

I'm not sure if this is the answer you are looking for or if it's already been answered for you... 
I'd use the catet character ( ^ ) in a batch file and output to a file using escape character ^. See examples..
Desired output...
<META content="text/html; charset=iso-8859-2" http-equiv=Content-Type> 

Replace code with this: 
Example 1: echo ^<META content="text/html; charset=iso-8859-2" http-equiv=Content-Type^> 
Example 2: echo ^<?xml version="1.0" encoding="utf-8" ?^>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM