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

namespace ChuPiao.Common.Utils
{
    /// <summary>
    /// 16进制字符串转换工具类（字符之间有空格）
    /// </summary>
    public class HexString
    {
        /// <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 * 3 - 1);
            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)
        {
            //inputString = inputString.Trim().Replace(" ",",");
            string[] arrInput;
            byte[] bytes;
            if(string.IsNullOrEmpty(inputString))
            {
                bytes=new byte[]{};
            }
            else
            {
                arrInput = inputString.Split(" ".ToCharArray());

                bytes = new byte[arrInput.Length];
                for (int i = 0; i < arrInput.Length; i++)
                {
                    bytes[i] = Convert.ToByte(arrInput[i], 16);
                }
            }
            return bytes;
        }
    }
}
