输入编码:接受UTF-8

And*_*ndy 7 powershell encoding

我需要在PowerShell下输出本机应用程序.问题是,输出使用UTF-8(无BOM)编码,PowerShell无法识别,只是将那些时髦的UTF字符直接转换为Unicode.

我发现PowerShell有$OutputEncoding变量,但它似乎不会影响输入数据.

好的'iconv也没有任何帮助,因为这个不必要的UTF8-as-if-ASCII => Unicode转换发生在下一个管道成员获取数据之前.

Kei*_*ill 12

我现在看到下面的程序问题(stdout.cpp - cl stdout.cpp):

#include <stdio.h>

void main()
{
    char bytes[] = { 0x41, 0x53, 0x43, 0x49, 
                     0x49, 0x20, 0x6F, 0x75, 
                     0x74, 0x70, 0x75, 0x74,
                     0xE1, 0xBE, 0xB9};

    for (int i = 0; i < 15; i++)
    {
        printf("%c", bytes[i]);
    }                
}
Run Code Online (Sandbox Code Playgroud)

并通过这种方式| Out-File -enc UTF8 foo.txt给出了胡言乱语:

PS> fhex foo.txt

Address:  0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F ASCII
-------- ----------------------------------------------- ----------------
00000000 EF BB BF 41 53 43 49 49 20 6F 75 74 70 75 74 0D ...ASCII output.
00000010 9F E2 95 9B E2 95 A3 0D 0A                      .........
Run Code Online (Sandbox Code Playgroud)

请注意,fhex是PSCX实用程序.

更新:弄清楚如何让它工作:

$enc = [Console]::OutputEncoding
[Console]::OutputEncoding = [text.encoding]::utf8
.\stdout.exe | out-file fubar3.txt -enc utf8
fhex .\fubar3.txt

Address:  0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F ASCII
-------- ----------------------------------------------- ----------------
00000000 EF BB BF 41 53 43 49 49 20 6F 75 74 70 75 74 E1 ...ASCII output.
00000010 BE B9 0D 0A                                     ....

[Console]::OutputEncoding = $enc
Run Code Online (Sandbox Code Playgroud)