转换文本编码

时间:2025-02-27 15:08:51

1 /// 2 /// 将文本转化为Unicode编码(自适应4种编码) 3 /// 4 /// 5 /// 6 public void ConvertTextToUnicode(string fileName) 7 { 8 Encoding encoding = null; 9 FileStream fs = (fileName, , ); 10 BinaryReader br = new BinaryReader(fs); 11 byte[] bs = (3); // 读文件的前个字节 12 (); 13 (); 14 15 // 确定编码 16 if ( > 0) 17 { 18 if (bs[0] == 0xEF && bs[1] == 0xBB && bs[2] == 0xBF) 19 { 20 encoding = Encoding.UTF8; 21 } 22 else if (bs[0] == 0xFF && bs[1] == 0xFE) 23 { 24 //encoding = ; 25 return; 26 } 27 else if (bs[0] == 0xFE && bs[1] == 0xFF) 28 { 29 encoding = ; 30 } 31 } 32 33 string text = (fileName, encoding ?? ); // 读取文本 34 (fileName, text, ); 35 }