c#中字符串編碼方式的轉換,附帶程序uft8到gb2312的互換

因而考慮在.net 平臺中轉換字符編碼。因而查看了.net中字符編碼的類System.Text.Encoding
裏面支持的字符集編碼有ibm850,沒有cp850,後來查看資料才知道原來這兩個名字指的是同一種編碼規範。
因而開始進行編碼轉換,首先找到一個java的程序:java

 

        public String CP850ToGB2312(String str)
        ...{
            try
            ...{
                byte[] temp = str.getBytes("cp850");
                String result = new String(temp, "gb2312");
                return result;
            }
            catch (UnsupportedEncodingException ex)
            ...{ return null; }
        }
        public String GB2312ToCP850(String str)
        ...{
            try
            ...{
                byte[] temp = str.getBytes("gb2312");
                String result = new String(temp, "cp850");
                return result;
            }
            catch (UnsupportedEncodingException ex)
            ...{
                return null;
            }
        }
 web

而後在根據查找的System.Text.Encoding類的屬性,方法寫了以下的轉換程序:編碼

 

 

        public string UTF8ToGB2312(string str)
        ...{
            try
            ...{    
                Encoding utf8 = Encoding.GetEncoding(65001);
                Encoding gb2312 = Encoding.GetEncoding("gb2312");//Encoding.Default ,936
                byte[] temp = utf8.GetBytes(str);
                byte[] temp1 = Encoding.Convert(utf8, gb2312, temp);
                string result = gb2312.GetString(temp1); 
                return result;
            }
            catch  (Exception ex)//(UnsupportedEncodingException ex)
            ...{
                MessageBox.Show(ex.ToString());
                return null; 
            }
        }
        public string GB2312ToUTF8(string str)
        ...{
            try
            ...{
                Encoding uft8 = Encoding.GetEncoding(65001);
                Encoding gb2312 = Encoding.GetEncoding("gb2312");
                byte[] temp = gb2312.GetBytes(str);
                MessageBox.Show("gb2312的編碼的字節個數:" + temp.Length);
                for (int i = 0; i < temp.Length; i++)
                ...{
                    MessageBox.Show(Convert.ToUInt16(temp[i]).ToString());
                }    
                byte[] temp1 = Encoding.Convert(gb2312, uft8, temp);
                MessageBox.Show("uft8的編碼的字節個數:" + temp1.Length);
                for (int i = 0; i < temp1.Length; i++)
                ...{
                    MessageBox.Show(Convert.ToUInt16(temp1[i]).ToString());
                }               
                string result = uft8.GetString(temp1);
                return result;
            }
            catch  (Exception ex)//(UnsupportedEncodingException ex)
            ...{
                MessageBox.Show(ex.ToString());
                return null;
            }
        }
 spa

主要使用的就是獲取編碼方式的類對象,
 Encoding utf8 = Encoding.GetEncoding(65001);//使用code page 
 Encoding gb2312 = Encoding.GetEncoding("gb2312");//經過bodyname
獲取字符編碼字節序列:byte[] temp=utf8.GetBytes(str);
編碼方式轉換:byte[] temp1=Encoding.Convert(utf8, gb2312, temp);
獲取編碼的字符串:string str1=gb2312.GetString(temp1); 
這樣即完成了字符編碼的轉換。
Encoding.Default在 簡體中文os中通常是gb2312格式。.net



        static void Main(string[] args)
        {
            FileStream fs;
            string fileName = "C://test.xml";
            string message = "呵呵";code

            string m=System.Web.HttpUtility.UrlEncode(message, System.Text.Encoding.UTF8);orm

            fs = new FileStream(fileName, FileMode.OpenOrCreate);
            StreamWriter sw = new StreamWriter(fs);
            fs.Seek(0, SeekOrigin.End);xml

            sw.WriteLine("<?xml version=/"1.0/" encoding=/"UTF-8/"?><menu>" + message + "</menu>");
            sw.Close();
            fs.Close();
            Console.Read();對象

           

        }
        private static string ToGB2312(string utfInfo)
        {
            string gb2312Info = string.Empty;
            Encoding utf8 = Encoding.UTF8;
            Encoding gb2312 = Encoding.GetEncoding("gb2312");
            byte[] unicodeBytes = utf8.GetBytes(utfInfo);
            byte[] asciiBytes = Encoding.Convert(utf8, gb2312, unicodeBytes);
            char[] asciiChars = new char[gb2312.GetCharCount(asciiBytes, 0, asciiBytes.Length)];
            gb2312.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);
            string gb2312info = new string(asciiChars);
            return gb2312info;
        }ci

        private static string ToUTF8(string gb2312Info)        {            string utf8Info = string.Empty;            Encoding utf8 = Encoding.UTF8;            Encoding gb2312 = Encoding.GetEncoding("gb2312");            byte[] unicodeBytes = gb2312.GetBytes(gb2312Info);            byte[] asciiBytes = Encoding.Convert(gb2312, utf8, unicodeBytes);            char[] asciiChars = new char[utf8.GetCharCount(asciiBytes, 0, asciiBytes.Length)];            utf8.GetChars(asciiBytes, 0, asciiBytes.Length, asciiChars, 0);            string utf8info = new string(asciiChars);            return utf8info;        }

相關文章
相關標籤/搜索