这两个命令是等效的,因为它们都默认使用 UTF-16 编码:
echo "string" > file.txt
echo "string" | out-file file.txt
您可以将显式编码参数添加到后一种形式(如 jon Z 所示)以生成纯 ASCII:
echo "string" | out-file -encoding ASCII file.txt
或者,您可以使用set-content
,默认情况下使用 ASCII 编码:
echo "string" | set-content file.txt
推论1:
想要在一行中将 unicode 文件转换为 ASCII?
只需使用这个:
get-content your_unicode_file | set-content your_ascii_file
可以缩写为:
gc your_unicode_file | sc your_ascii_file
推论2:
想要获得一个十六进制转储,以便您真正了解什么是 unicode 和什么是 ASCII?
使用 PowerShell.com 上提供的简洁的Get-HexDump函数。有了它,您可以检查生成的文件:
Get-HexDump file.txt
对于任何重要的事情,您可以指定要输出的列宽以及要处理的文件字节数,如下所示:
Get-HexDump file.txt -width 15 -bytes 150