2012年3月22日 星期四

Convert Big5 To Unicode for windows phone 7

參考:http://forums.create.msdn.com/forums/p/73736/553266.aspx

研究一下發現wp7的library中只提供了Unicode和Utf-8兩種編碼方式,
所以如果要使用網路上big5編碼的文字就要自己轉換了 不然會變亂碼

參考了MicroStrategy的文章之後
自己再重新包裝了一個工具類
需要先下載 這個編碼轉換的文字檔  導入專案中


之後把網路上抓回的Stream(big5)傳進去
就可以輸出想要的Sting(unicode)了~



public class Big5Util
{

    private static Dictionary<int, int> mBIG5_Unicode_MAP = new Dictionary<int, int> ();

    static Big5Util ()
    {
        StreamResourceInfo _ResourceInfo = Application.GetResourceStream (new Uri ("MyDemos;component/Resource/BIG5.TXT", UriKind.RelativeOrAbsolute));

        StreamReader _Reader = new StreamReader (_ResourceInfo.Stream);


        String line;

        while ((line = _Reader.ReadLine ()) != null)
        {
            if (line.StartsWith ("#"))
                continue; // Comments
            string[] lTokens = line.Split (new char[] { '\t' });
            if (lTokens.Length < 2)
                continue; // Not enough tokens
            try
            {
                mBIG5_Unicode_MAP.Add (int.Parse (lTokens[0].Substring (2), NumberStyles.HexNumber), int.Parse (lTokens[1].Substring (2), NumberStyles.HexNumber));
            }
            catch (Exception)
            {
                continue; // No mapping
            }
        }
    }

    public static string ToUni (Stream _str)
    {
        StringBuilder _StringBuilder = new StringBuilder ();
        byte[] big5Buffer = new byte[2];

        int input;
        while ((input = _str.ReadByte ()) != -1)
        {

            if (input > 0x81 && big5Buffer[0] == 0) //lead byte of big 5
            {
                big5Buffer[0] = (byte)input;
            }
            else if (big5Buffer[0] != 0)
            {
                big5Buffer[1] = (byte)input;
                int Big5Char = (big5Buffer[0] << 8) + big5Buffer[1];
                try
                {
                    int UTF8Char = mBIG5_Unicode_MAP[Big5Char];
                    _StringBuilder.Append ((char)UTF8Char);
                }
                catch (Exception)
                {

                    _StringBuilder.Append ((char)mBIG5_Unicode_MAP[0xA148]); // No mapping, use replacement character
                }

                big5Buffer = new byte[2];
            }
            else
            {
                _StringBuilder.Append ((char)input); // ASCII character
            }
        }
        return _StringBuilder.ToString ();
    }
}

2 則留言: