using System;
using System.Collections.Generic;
using System.Text;

namespace ChuPiao.Common.Utils
{
    /// <summary>
    /// 16进制字符串转换工具类（字符之间无空格）
    /// </summary>
    public class HexStringUtil
    {
        /// <summary>
        /// 字节转换为16进制字符串
        /// </summary>
        /// <param name="inputBytes"></param>
        /// <returns></returns>
        public static string ByteToString(byte[] inputBytes)
        {
            if (inputBytes == null || inputBytes.Length < 1)
            {
                return string.Empty;
            }
            StringBuilder stringBuilder = new StringBuilder(inputBytes.Length * 2);
            foreach (byte b in inputBytes)
            {
                stringBuilder.Append(String.Format("{0:X2}", b));
            }

            return stringBuilder.ToString().Trim();
        }
        /// <summary>
        /// 16进制字符串转换为字节
        /// </summary>
        /// <param name="inputString"></param>
        /// <returns></returns>
        public static byte[] StringToByte(string inputString)
        {
            byte[] bytes;
            if (string.IsNullOrEmpty(inputString))
            {
                bytes = new byte[] { };
            }
            else
            {
                int length = inputString.Length / 2;
                bytes = new byte[length];
                for (int i = 0; i < length; i++)
                {
                    bytes[i] = Convert.ToByte(inputString.Substring(i * 2, 2), 16);
                }
            }
            return bytes;
        }

        /// <summary>
        /// <函数：Encode>
        /// 作用：将字符串内容转化为16进制数据编码，其逆过程是Decode
        /// 参数说明：
        /// strEncode 需要转化的原始字符串
        /// 转换的过程是直接把字符转换成Unicode字符,比如数字"3"-->0033,汉字"我"-->U+6211
        /// 函数decode的过程是encode的逆过程.
        /// </summary>
        /// <param name="strEncode"></param>
        /// <returns></returns>
        public static string Encode(string strEncode)
        {
            string strReturn = "";//　存储转换后的编码
            foreach (short shortx in strEncode.ToCharArray())
            {
                strReturn += shortx.ToString("X4");
            }
            return strReturn;
        }
        /// <summary>
        /// <函数：Decode>
        ///作用：将16进制数据编码转化为字符串，是Encode的逆过程
        /// </summary>
        /// <param name="strDecode"></param>
        /// <returns></returns>
        public static string Decode(string strDecode)
        {
            string sResult = "";
            for (int i = 0; i < strDecode.Length / 4; i++)
            {
                sResult += (char)short.Parse(strDecode.Substring(i * 4, 4), global::System.Globalization.NumberStyles.HexNumber);
            }
            return sResult;
        }
    }
}
