分享

Gb2312Encoding 编码转换

 alaricyyy 2015-10-29
using System;
using System.Collections.Generic;
using System.Text;

namespace BaiduZhidao_轴承
{
public class Gb2312Encoding : Encoding
{
public Gb2312Encoding ()
{
}

public override string WebName
{
get
{
return "gb2312";
}
}

public override int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
throw new NotImplementedException();
}

public override int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
{
int j = 0;
char c;
for (int i = 0; i < byteCount; i += 2)
{
if (i + 1 >= bytes.Length)
{
char[] last = Encoding.UTF8.GetChars(new byte[] { bytes[i] });
chars[j]=last[0];
}
else
{
byte[] bb = new byte[] { bytes[i], bytes[i + 1] };
if (Gb2312toUnicodeDictinary.TryGetChar(bb, out c))
{
chars[j] = c;
j++;
}
else
{
char[] tt = Encoding.UTF8.GetChars(new byte[] { bb[1] });
chars[j] = tt[0];
j++;
//测试下一个
if (i + 2 >= bytes.Length)
{
char[] tttt = Encoding.UTF8.GetChars(new byte[] { bb[0] });
chars[j] = tttt[0];
j++;
}
else
{
byte[] test = new byte[] { bb[0], bytes[i + 2] };
if (Gb2312toUnicodeDictinary.TryGetChar(test, out c))
{
chars[j] = c;
j++;
i++;
}
else
{
char[] ttt = Encoding.UTF8.GetChars(new byte[] { bb[0] });
chars[j] = ttt[0];
j++;
}

}
}
}
}

return chars.Length;
}

public override int GetByteCount(char[] chars, int index, int count)
{
return count;
}
public override int GetCharCount(byte[] bytes, int index, int count)
{
return count;
}

public override int GetMaxByteCount(int charCount)
{
return charCount;
}
public override int GetMaxCharCount(int byteCount)
{
return byteCount;
}
public static int CharacterCount
{
get { return 7426; }
}

//...

}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多