C#读取中文文件乱码的解方法


当前第2页 返回上一页

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

private void DetectEncoding()

{

    if (this.byteLen >= 2)

    {

        this._detectEncoding = false;

        bool flag = false;

        if ((this.byteBuffer[0] == 0xfe) && (this.byteBuffer[1] == 0xff))

        {

            this.encoding = new UnicodeEncoding(true, true);

            this.CompressBuffer(2);

            flag = true;

        }

        else if ((this.byteBuffer[0] == 0xff) && (this.byteBuffer[1] == 0xfe))

        {

            if (((this.byteLen < 4) || (this.byteBuffer[2] != 0)) || (this.byteBuffer[3] != 0))

            {

                this.encoding = new UnicodeEncoding(false, true);

                this.CompressBuffer(2);

                flag = true;

            }

            else

            {

                this.encoding = new UTF32Encoding(false, true);

                this.CompressBuffer(4);

                flag = true;

            }

        }

        else if (((this.byteLen >= 3) && (this.byteBuffer[0] == 0xef)) && ((this.byteBuffer[1] == 0xbb) && (this.byteBuffer[2] == 0xbf)))

        {

            this.encoding = Encoding.UTF8;

            this.CompressBuffer(3);

            flag = true;

        }

        else if ((((this.byteLen >= 4) && (this.byteBuffer[0] == 0)) && ((this.byteBuffer[1] == 0) && (this.byteBuffer[2] == 0xfe))) && (this.byteBuffer[3] == 0xff))

        {

            this.encoding = new UTF32Encoding(true, true);

            this.CompressBuffer(4);

            flag = true;

        }

        else if (this.byteLen == 2)

        {

            this._detectEncoding = true;

        }

        if (flag)

        {

            this.decoder = this.encoding.GetDecoder();

            this._maxCharsPerBuffer = this.encoding.GetMaxCharCount(this.byteBuffer.Length);

            this.charBuffer = new char[this._maxCharsPerBuffer];

        }

    }

}

返回前面的内容

相关阅读 >>

c#访问sqlserver的工具类sqlserverhelper

c#查找字符串的所有排列组合

c#读取host文件代码

c#实现图标锁定到windows任务栏或删除图标

c#自定读取配置文件类

c#读取中文文件乱码的解方法

c#操作mysql的工具类mysqlhelper

c# 四舍五入round函数使用

c#中登陆账户使用的md5加密算法

c# 识别url是否是网络路径

更多相关阅读请进入《代码片段》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...