星期三, 1月 10, 2007

FILE的IO與Encoding

常用的IO class StreamReader就可以設定Encoding
StreamReader sr = new StreamReader("dic/wordlist_utf8.txt", Encoding.UTF8)

至於要把程式內的utf8 string轉成其它Encoding,以下是msdn的示範
string unicodeString = "This string contains the unicode character Pi(\u03a0)喔 是這樣嗎";

// Create two different encodings.
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;

// Convert the string into a byte[].
byte[] unicodeBytes = unicode.GetBytes(unicodeString);

// Perform the conversion from one encoding to the other.
byte[] asciiBytes = Encoding.Convert(unicode, ascii, unicodeBytes);

// Convert the new byte[] into a char[] and then into a string.
// This is a slightly different approach to converting to illustrate
// the use of GetCharCount/GetChars.
char[] asciiChars = new char[ascii.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
ascii.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
string asciiString = new string(asciiChars);

// Display the strings created before and after the conversion.
Console.WriteLine("Original string: {0}", unicodeString);
Console.WriteLine("Ascii converted string: {0}", asciiString);
簡單的說,就是利用Encoding.Convert轉成不同encoding的byte[],然後再把byte[]轉成char[]或是string。