日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

C#?Unicode編碼解碼的實(shí)現(xiàn)_C#教程

作者:香煎三文魚(yú) ? 更新時(shí)間: 2022-08-12 編程語(yǔ)言

Unicode是計(jì)算機(jī)科學(xué)領(lǐng)域里的一項(xiàng)業(yè)界標(biāo)準(zhǔn),包括字符集、編碼方案等。Unicode 是為了解決傳統(tǒng)的字符編碼方案的局限而產(chǎn)生的,它為每種語(yǔ)言中的每個(gè)字符設(shè)定了統(tǒng)一并且唯一的二進(jìn)制編碼,以滿足跨語(yǔ)言、跨平臺(tái)進(jìn)行文本轉(zhuǎn)換、處理的要求。

在這里插入圖片描述

在表示一個(gè)Unicode的字符時(shí),通常會(huì)用“U+”然后緊接著一組十六進(jìn)制的數(shù)字來(lái)表示這一個(gè)字符。在 基本多文種平面里的所有字符,要用四位十六進(jìn)制數(shù);在零號(hào)平面以外的字符則需要使用五位或六位十六進(jìn)制數(shù)了。

string str = @"\u0005 \u0002\U00f3 \U +e9\u00e9";
string newStr = UnicodeDecode(str);
Console.WriteLine(newStr);
Console.WriteLine();

newStr = ToUnicode("0 - * @ , 。 ? 真的 繁體字");
Console.WriteLine(newStr);
Console.WriteLine();

正常字符轉(zhuǎn)換為unicode

        /// <summary>
        /// 對(duì)正常的字符串轉(zhuǎn)換為 Unicode 的字符串
        /// </summary>
        /// <param name="normalStr">正常的字符串</param>
        /// <param name="isIgnoreSpace">是否忽略空格符;默認(rèn) true 空格符不轉(zhuǎn)換;false 空格符要轉(zhuǎn)換</param>
        /// <param name="isUpperCaseU">是否大寫(xiě)U字母 ‘\U';默認(rèn) false ‘\u'</param>
        /// <returns></returns>
        public string ToUnicode(this string normalStr, bool isIgnoreSpace = true, bool isUpperCaseU = false)
        {
            if (string.IsNullOrEmpty(normalStr))
            {
                return string.Empty;
            }

            StringBuilder strResult = new StringBuilder();

            void func(int index)
            {
                if (isUpperCaseU)
                {
                    strResult.Append("\\U");
                }
                else
                {
                    strResult.Append("\\u");
                }
                strResult.Append(((int)normalStr[index]).ToString("x").PadLeft(4, '0'));
            }

            for (int i = 0; i < normalStr.Length; i++)
            {
                if (isIgnoreSpace)
                {
                    if (normalStr[i] == ' ')
                    {
                        strResult.Append(" ");
                    }
                    else
                    {
                        func(i);
                    }
                }
                else
                {
                    func(i);
                }
            }
            return strResult.ToString();
        }

解碼

        /// <summary>
        /// 對(duì) Unicode 的字符串解碼
        /// </summary>
        /// <param name="unicodeStr">Unicode 字符串</param>
        /// <returns></returns>
        public string UnicodeDecode(string unicodeStr)
        {
            if (string.IsNullOrWhiteSpace(unicodeStr) || (!unicodeStr.Contains("\\u") && !unicodeStr.Contains("\\U")))
            {
                return unicodeStr;
            }

            string newStr = Regex.Replace(unicodeStr, @"\\[uU](.{4})", (m) =>
            {
                string unicode = m.Groups[1].Value;
                if (int.TryParse(unicode, System.Globalization.NumberStyles.HexNumber, null, out int temp))
                {
                    return ((char)temp).ToString();
                }
                else
                {
                    return m.Groups[0].Value;
                }
            }, RegexOptions.Singleline);

            return newStr;
        }

原文鏈接:https://blog.csdn.net/qq_39569480/article/details/125082358

欄目分類
最近更新