\u格式 &# unicode与汉字的相互转换

直接上代码

using System;
using System.Text;
using System.Text.RegularExpressions;

namespace UnicodeConvertDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            string source = string.Empty;
            //字符串转unicode
            source = "合资";
            string hexUnicodeString = String2HexUnicode(source);
            Console.WriteLine("字符串【{0}】转16进制unicode结果:{1}", source, hexUnicodeString);
            string decUnicodeString = String2DecUnicode(source);
            Console.WriteLine("字符串【{0}】转10进制unicode结果:{1}", source, decUnicodeString);

            //unicode转字符串
            string result = string.Empty;
            result = HexUnicode2String(hexUnicodeString);
            Console.WriteLine("16进制unicode【{0}】转字符串结果:{1}", hexUnicodeString, result);
            result = DecUnicode2String(decUnicodeString);
            Console.WriteLine("10进制unicode【{0}】转字符串结果:{1}", decUnicodeString, result);
        }

        /// 
        /// 字符串转Unicode(16进制)
        /// 
        /// 源字符串
        /// Unicode编码后的字符串
        static string String2HexUnicode(string source)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(source);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i += 2)
            {
                stringBuilder.AppendFormat("\\u{0}{1}", bytes[i + 1].ToString("x").PadLeft(2, '0'), bytes[i].ToString("x").PadLeft(2, '0'));
            }
            return stringBuilder.ToString();
        }

        /// 
        /// 字符串转Unicode(10进制)
        /// 
        /// 源字符串
        /// Unicode编码后的字符串
        static string String2DecUnicode(string source)
        {
            byte[] bytes = Encoding.Unicode.GetBytes(source);
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < bytes.Length; i += 2)
            {
                string tmp = bytes[i + 1].ToString("x").PadLeft(2, '0') + bytes[i].ToString("x").PadLeft(2, '0');
                stringBuilder.AppendFormat("&#{0}", Int32.Parse(tmp, System.Globalization.NumberStyles.HexNumber));
            }
            return stringBuilder.ToString();
        }

        /// 
        /// Unicode(16进制)转字符串
        /// 
        /// 经过Unicode编码的字符串
        /// 正常字符串
        static string HexUnicode2String(string source)
        {
            return new Regex(@"\\u([0-9A-F]{4})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace(
                source, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result("$1"), 16)));
        }

        /// 
        /// Unicode(10进制)转字符串
        /// 
        /// 经过Unicode编码的字符串
        /// 正常字符串
        static string DecUnicode2String(string source)
        {
            return new Regex(@"&#([0-9]{5})", RegexOptions.IgnoreCase | RegexOptions.Compiled).Replace(
                source, x => string.Empty + Convert.ToChar(Convert.ToUInt16(x.Result("$1"))));
        }

    }
}

你可能感兴趣的:(.Net,字符转换)