2014-01-15 52 views
3
byte[] newBytes = new Byte[] { 169 }; 
string string1 = System.Text.Encoding.UTF8.GetString(newBytes, 0, newBytes.Length); 

在上面的程序中,我期望string1具有版權符號©的值。爲什麼我的c#代碼不能識別版權符號?

,但我得到一些其他的值(可能是一些垃圾),如下圖所示

enter image description here

我有什麼錯?

+0

參見:http://stackoverflow.com/questions/644785/is-it-possible-to-get-a-copyright- symbol-in-c-sharp-console-application –

+2

錯誤的編碼,即不是utf-8。 Encoding.Default或Encoding.GetEncoding(1252)有工作的可能性。 –

回答

7

UTF8需要多字節編碼大於127的特徵點。如果你運行的反向,你就會明白,它預計:

System.Text.Encoding.UTF8.GetBytes("©"); // { 194, 169 } 

試試這個:

byte[] newBytes = new Byte[] { 194, 169 }; 
string string1 = System.Text.Encoding.UTF8.GetString(newBytes, 0, newBytes.Length); 

如果你絕對要使用原始字節數組,你需要選擇不同的編碼。例如,Windows-1252編碼使用一個單一的字節來編碼版權符號:

byte[] newBytes = new Byte[] { 169 }; 
var encoding = Encoding.GetEncoding(1252); 
string string1 = encoding.GetString(newBytes, 0, newBytes.Length); // "©" 
相關問題