cmd.exe 中的 UTF-16 檔案輸出

cmd.exe 中的 UTF-16 檔案輸出
chcp 10000
echo hell☺ w☻rld♥! >> "UTF-16 file☺☻♥♦♣♠"

好的,它創建了正確的文件,但內容中有問號而不是 unicode 字元。如何修復它?

答案1

您的程式碼不正確,因為 10000 不是 Unicode 代碼頁。看代碼頁標識符

10000   macintosh   MAC Roman; Western European (Mac)
...
1200    utf-16      Unicode UTF-16, little endian byte order (BMP of ISO 10646); available only to managed applications
1201    unicodeFFFE Unicode UTF-16, big endian byte order; available only to managed applications
...
12000   utf-32      Unicode UTF-32, little endian byte order; available only to managed applications
12001   utf-32BE    Unicode UTF-32, big endian byte order; available only to managed applications
...
65000   utf-7       Unicode (UTF-7)
65001   utf-8       Unicode (UTF-8)

我不知道為什麼,但貼上時命令提示字元似乎將它們解釋為控製字符,特別是 SOH(標題開始,01)、STX(文字開始,02)和 ETX(文字結束,03)。

從好的方面來說,PowerShell 似乎可以正確處理這個問題。 Notepad++ 會自動將產生的文字檔案開啟為“UCS-2 Little Endian”,並顯示正確的字元。


好吧,我已經弄清楚為什麼 UTF-8 不適合我了。字體應設定為 Lucida Console,因為預設光柵字體不支援 Unicode。

答案2

65001.txt和都1200.txt包含相同的字串:абв™但編碼不同。命令:

chcp 65001 & type 65001.txt 

成功更改代碼頁,但顯示垃圾。

命令:

type 1200.txt

顯示正確的字符,但命令

for /f %A in ('type 1200.txt') do echo %A

顯示абвT.

因此 cmd.exe 能夠使用代碼頁 1200(有一些限制),而我無法使用代碼頁 65001 獲得任何令人滿意的結果。

相關內容