從16進制轉換成漢字


 

 Demo  轉載:http://www.cnblogs.com/hantianwei/archive/2009/09/21/1571264.html  未測試

/// <summary>
    /// 從漢字轉換到16進制
    /// </summary>
    /// <param name="s"></param>
    /// <returns></returns>
    public static string GetHexFromChs(string s)
    {
        if ((s.Length % 2) != 0)
        {
            s += " ";//空格
            //throw new ArgumentException("s is not valid chinese string!");
        }

        System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");

        byte[] bytes = chs.GetBytes(s);

        string str = "";

        for (int i = 0; i < bytes.Length; i++)
        {
            str += string.Format("{0:X}", bytes[i]);
        }

        return str;
    }
    /// <summary>
    /// 從16進制轉換成漢字
    /// </summary>
    /// <param name="hex"></param>
    /// <returns></returns>
    public static string GetChsFromHex(string hex)
    {
        if (hex == null)
            throw new ArgumentNullException("hex");
        if (hex.Length % 2 != 0)
        {
            hex += "20";//空格
            //throw new ArgumentException("hex is not a valid number!", "hex");
        }
        // 需要將 hex 轉換成 byte 數組。
        byte[] bytes = new byte[hex.Length / 2];

        for (int i = 0; i < bytes.Length; i++)
        {
            try
            {
                // 每兩個字符是一個 byte。
                bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
                    System.Globalization.NumberStyles.HexNumber);
            }
            catch
            {
                // Rethrow an exception with custom message.
                throw new ArgumentException("hex is not a valid hex number!", "hex");
            }
        }

        // 獲得 GB2312,Chinese Simplified。
        System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");


        return chs.GetString(bytes);
    }

  

 

        /// <summary>
        /// 從16進制轉換成漢字
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        public static string GetChsFromHex(string hex)
        {
            if (hex == null)
                throw new ArgumentNullException("hex");
            if (hex.Length % 2 != 0)
            {
                hex += "20";//空格
                //throw new ArgumentException("hex is not a valid number!", "hex");
            }
            // 需要將 hex 轉換成 byte 數組。
            byte[] bytes = new byte[hex.Length / 2];

            for (int i = 0; i < bytes.Length; i++)
            {
                try
                {
                    // 每兩個字符是一個 byte。
                    bytes[i] = byte.Parse(hex.Substring(i * 2, 2),
                        System.Globalization.NumberStyles.HexNumber);
                }
                catch
                {
                    // Rethrow an exception with custom message.
                    throw new ArgumentException("hex is not a valid hex number!", "hex");
                }
            }

            // 獲得 GB2312,Chinese Simplified。
            System.Text.Encoding chs = System.Text.Encoding.GetEncoding("gb2312");


            return chs.GetString(bytes).Replace("\0","");
        }

 


免責聲明!

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



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