C# Unicode編碼解碼的實(shí)現(xiàn)
Unicode是計(jì)算機(jī)科學(xué)領(lǐng)域里的一項(xiàng)業(yè)界標(biāo)準(zhǔn),包括字符集、編碼方案等。Unicode 是為了解決傳統(tǒng)的字符編碼方案的局限而產(chǎn)生的,它為每種語言中的每個字符設(shè)定了統(tǒng)一并且唯一的二進(jìn)制編碼,以滿足跨語言、跨平臺進(jìn)行文本轉(zhuǎn)換、處理的要求。
在表示一個Unicode的字符時,通常會用“U+”然后緊接著一組十六進(jìn)制的數(shù)字來表示這一個字符。在 基本多文種平面里的所有字符,要用四位十六進(jìn)制數(shù);在零號平面以外的字符則需要使用五位或六位十六進(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> /// 對正常的字符串轉(zhuǎn)換為 Unicode 的字符串 /// </summary> /// <param name="normalStr">正常的字符串</param> /// <param name="isIgnoreSpace">是否忽略空格符;默認(rèn) true 空格符不轉(zhuǎn)換;false 空格符要轉(zhuǎn)換</param> /// <param name="isUpperCaseU">是否大寫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> /// 對 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; }
到此這篇關(guān)于C# Unicode編碼解碼的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)C# Unicode編碼解碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- C# 字符串與unicode互相轉(zhuǎn)換實(shí)戰(zhàn)案例
- C# 讀取ttf字體文件里的Unicode實(shí)現(xiàn)
- C#把UNICODE編碼轉(zhuǎn)換為GB編碼的實(shí)例
- C#將Unicode編碼轉(zhuǎn)換為漢字字符串的簡單方法
- c# 實(shí)現(xiàn)獲取漢字十六進(jìn)制Unicode編碼字符串的實(shí)例
- C#實(shí)現(xiàn)將漢字轉(zhuǎn)化為2位大寫的16進(jìn)制Unicode的方法
- C#實(shí)現(xiàn)Json轉(zhuǎn)Unicode的方法
- c#中文轉(zhuǎn)unicode字符示例分享
- C#隨機(jī)生成Unicode類型字符串
相關(guān)文章
C#實(shí)現(xiàn)將Word轉(zhuǎn)化分享為電子期刊
曾經(jīng)由一個項(xiàng)目,要求實(shí)現(xiàn)制作電子期刊定期發(fā)送給企業(yè)進(jìn)行閱讀,由編輯人員使用 Microsoft Word先生成PDF文件,然后將生成的PDF文件轉(zhuǎn)化為JPEG文件,最后將JPEG文件生成電子書模式,本文給大家介紹了C#實(shí)現(xiàn)將Word轉(zhuǎn)化分享為電子期刊,需要的朋友可以參考下2023-12-12Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲(2)
這篇文章主要為大家詳細(xì)介紹了Unity3D實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲的第二部分,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06C#把UNICODE編碼轉(zhuǎn)換為GB編碼的實(shí)例
下面小編就為大家?guī)硪黄狢#把UNICODE編碼轉(zhuǎn)換為GB編碼的實(shí)例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01