using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DotNettyFrom.util
{
    public class HexUtil
    {
        public static byte[] hexToBytes(string hex)
        {
            hex = hex.Replace(" ", "").Replace("\r", "").Replace("\t", "").Replace("\n", "");
            int len = hex.Length / 2;
            byte[] bs = new byte[len];
            for (int i = 0; i < len; i++)
            {
                string sub = hex.Substring(2 * i, 2);
                //                String sub = hex.substring(2 * i, 2 * i + 2);
                int v = Utils.hexToTen(sub);
                //                int v = Integer.valueOf(sub, 16);
                bs[i] = (byte) v;
            }

            return bs;
        }

        public static String byteToHexStr(byte[] bs)
        {
            int len = bs.Length;
            StringBuilder sb = new StringBuilder(len * 2);
            for (int i = 0; i < len; i++)
            {
                byte b = bs[i];
                string str = Convert.ToString(b, 16);
                //                String str = Integer.toHexString(b);
                if (str.Length == 1)
                {
                    sb.Append("0").Append(str);
                }
                else if (str.Length > 2)
                {
                    sb.Append(str.Substring(str.Length - 2));
                }
                else
                {
                    sb.Append(str);
                }
            }

            return sb.ToString();
        }
    }
}