下面小編就為大家帶來一篇c# 實現擷取漢字十六進位Unicode編碼字串的執行個體。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
1、漢字轉十六進位UNICODE編碼字串
/// <summary> /// //// /// </summary> /// <param name="character"></param> /// <returns></returns> public string CharacterToCoding(string character) { string coding = ""; for (int i = 0; i < character.Length; i++) { byte[] bytes = System.Text.Encoding.Unicode.GetBytes(character.Substring(i, 1)); //取出二進位編碼內容 string lowCode = System.Convert.ToString(bytes[0], 16); //取出低位元組編碼內容(兩位16進位) if (lowCode.Length == 1) { lowCode = "0" + lowCode; } string hightCode = System.Convert.ToString(bytes[1], 16); //取出高位元組編碼內容(兩位16進位) if (hightCode.Length == 1) { hightCode = "0" + hightCode; } coding += (hightCode + lowCode); } return coding; }
2、 十六進位UNICODE編碼字串 轉 漢字
/// <summary> /// // /// </summary> /// <param name="text"></param> /// <returns></returns> public string UnicodeToCharacter(string text) { byte[] arr = HexStringToByteArray(text); System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding(); string str = converter.GetString(arr); return str; }