echo “string” > file in Windows PowerShell appends non-printable character to the file

后端 未结 3 574
逝去的感伤
逝去的感伤 2020-12-18 06:14

In Windows PowerShell:

echo \"string\" > file.txt

In Cygwin:

$ cat file.txt
:::s t r i         


        
3条回答
  •  失恋的感觉
    2020-12-18 06:48

    Try echo "string" | out-file -encoding ASCII file.txt to get a simple ASCII-encoded txt file.

    Comparison of the files produced:

    echo "string" | out-file -encoding ASCII file.txt
    

    will produce a file with the following contents:

    73 74 72 69 6E 67 0D 0A (string..)
    

    however

    echo "string" > file.txt
    

    will produce a file with the following contents:

    FF FE 73 00 74 00 72 00 69 00 6E 00 67 00 0D 00 0A 00 (ÿþs.t.r.i.n.g.....)
    

    (Byte order mark FF FE indicates the file is UTF-16 (LE). The signature for UTF-16 (LE) = 2 bytes: 0xFF 0xFE followed by 2 byte pairs. xx 00 xx 00 xx 00 for normal 0-127 ASCII chars

提交回复
热议问题