Powershell汉字编码错误

时间:2017-02-02 08:45:41

标签: powershell encoding character-encoding

我有一个名为test.txt的文件,其中包含一个中文字符

此字符看起来像这样

enter image description here

在hex-editor的视图下。

如果我get-content test.txt | Out-File test_output.txt,则test_output.txt的内容与test.txt不同。为什么要这样做?

我已经尝试了所有列出的编码参数here(" Unicode"," UTF7"," UTF8",&#34 ; UTF32"," ASCII"," BigEndianUnicode","默认"和" OEM"),但没有一个正确转换中国人。

如何使用Get-ContentOut-File正确转换中文字符?

编码e4 b8 ad看起来像的{​​{3}},这就是为什么所有编码参数都与这个中文字符不兼容的原因?

我分别使用URLencode和Notepad ++' Notepad++作为我的文本编辑器和十六进制编辑器。

2 个答案:

答案 0 :(得分:0)

我尝试了get-content test.txt -encoding UTF8 | Out-File test_output.txt -encoding UTF8

我的test.txt是“ e4 b8 ad 0a”。输出结果是“ef bb bf e4 b8 ad 0d 0a”

test.txt是UTF-8。

除非使用BOM,否则

Get-Content无法识别UTF-8。 Out-File默认使用UTF-16。

因此,必须为两个命令指定编码

答案 1 :(得分:0)

就我而言, Unicode 编码解决了我的汉字问题。我正在修改的文件在TFS服务器上包含一个C#代码。

$path="test.cs"
Get-Content -Path $path -Encoding Unicode
Set-Content -Path $path -Encoding Unicode

这可能会帮助其他人。

相关问题