C# - 漢字與unicode之間的轉換


/// <summary>
    /// 字符串轉Unicode碼
    /// </summary>
    /// <returns>The to unicode.</returns>
    /// <param name="value">Value.</param>
    private string StringToUnicode(string value)
    {
        byte[] bytes = Encoding.Unicode.GetBytes (value);
        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 ();
    }

    /// <summary>
    /// Unicode轉字符串
    /// </summary>
    /// <returns>The to string.</returns>
    /// <param name="unicode">Unicode.</param>
    private string UnicodeToString(string unicode)
    {
        string resultStr = "";
        string[] strList = unicode.Split ('u');
        for (int i = 1; i < strList.Length; i++) {
            resultStr += (char)int.Parse (strList[i], System.Globalization.NumberStyles.HexNumber);
        }
        return resultStr;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM