C#中Encoding.Unicode與Encoding.UTF8的區別


今天在園子首頁看到一篇博文-簡單聊下Unicode和UTF-8,從中知道了UTF-8是Unicode的一種實現方式:

Unicode只是給這世界上每個字符規定了一個統一的二進制編號,並沒有規定程序該如何去存儲和解析。

可以說UTF-8是Unicode實現方式之一...

在閃存中記錄這個收獲時,@飛鳥_Asuka在回復中提了一個很好的問題:“那么在選擇編碼方式的時候為什么unicode和utf8會是分別的兩個選項呢?”

在C#中,System.Text.Encoding.Unicode與System.Text.Encoding.UTF8分別是2種編碼方式。如果UTF-8是Unicode的一種實現方式,那C#中為什么將Encoding.Unicode作為與UTF8並列的一種編碼方式呢?

后來在stackoverflow上找到了答案

Windows handles so-called "Unicode" strings as UTF-16 strings, while most UNIXes default to UTF-8 these days. 

原來Windows默認的Unicode實現是UTF-16,所以C#中Encoding.Unicode就是UTF-16。

System.Text.Encoding.Unicode的注釋也證明了這一點:

//
// Summary:
//     Gets an encoding for the UTF-16 format using the little endian byte order.
//
// Returns:
//     An encoding for the UTF-16 format using the little endian byte order.
public static Encoding Unicode { get; }

C#中,Encoding.Unicode = UTF-16 。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM