C#.NET GB2312編碼轉化為中文
阿新 • • 發佈:2019-02-07
程式碼:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; namespace GB2312ConvertChinese { class Program { static void Main(string[] args) { string content = "/HTC Butterfly X920e是剛剛推出不久的一款智慧手機,該機的5.0英寸大屏&"; Console.WriteLine(GBKToChinese(content)); Console.ReadKey(); } static string GBKToChinese(string content) { string pattern = "&#(.+?);"; if (!Regex.IsMatch(content, pattern)) return content; MatchCollection collection = Regex.Matches(content, pattern); foreach (Match item in collection) { string GBK_Code = item.Value; string GBK_Value = item.Groups[1].Value; string chinese = ((char)Convert.ToInt32(GBK_Value, 10)).ToString(); content = content.Replace(GBK_Code, chinese); } return content; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { //獲得GB2312編碼。 static void Main(string[] args) { string str = "研究中心"; StringBuilder sb = new StringBuilder(); foreach (char c in str) sb.AppendFormat("&#{0};", Convert.ToString((int)c, 10)); Console.WriteLine(sb.ToString()); Console.ReadKey(); } } }
參考:請問關於\u開頭的漢字編碼是什麼型別的編碼,c#怎樣轉換
unicode字元
如果是這樣的string str = "\u5317\u4eac";
那直接輸出就可以了
如果這個字串是從其它地方得到的話,就這樣:
- C# code
-
string str ="\\u5317\\u4eac";
string[] temp = str.Split(newstring[] { "\\u" }, StringSplitOptions.RemoveEmptyEntries);
for (int i =0; i < temp.Length; i++
要從漢字轉成unicode編碼:
- C# code
- string str ="北京"; StringBuilder sb =new StringBuilder(); foreach (char c in str) sb.AppendFormat("\\u{0}", Convert.ToString((int)c, 16)); Response.Write(sb.ToString());