2015-10-13 161 views
1

我如何可以將cp1252字符串轉換爲utf-8字符串在c#中? 我嘗試這樣的代碼,但它不工作:cp1252字符串編碼爲utf-8在c#中的字符串

Encoding wind1252 = Encoding.GetEncoding(1252); 
Encoding utf8 = Encoding.GetEncoding(1251); 
byte[] wind1252Bytes = ReadFile(myString1252); 
byte[] utf8Bytes = Encoding.Convert(wind1252, utf8, wind1252Bytes); 
string myStringUtf8 = Encoding.UTF8.GetString(utf8Bytes); 
+1

'編碼UTF8 = Encoding.GetEncoding(1251);'代碼頁1251是不是UTF8。 – stuartd

+0

'編碼utf8 = Encoding.UTF8;' – Equalsk

回答

1
var myGoodString = System.IO.File.ReadAllText(
    @"C:\path\to\file.txt", 
    Encoding.GetEncoding("Windows-1252") 
    ); 

一個.NET/CLRstring在內存不能爲UTF-8。它只是Unicode,或者如果你喜歡,就是UTF-16。

上述代碼將正確讀取CP1252中的文本文件,並將其轉換爲.NET string

如果你堅持通過byte[] wind1252Bytes去,這簡直是:

var myGoodString = Encoding.GetEncoding("Windows-1252").GetString(wind1252Bytes);