using System.Runtime.InteropServices;
using System.Text;

namespace UHFHelper
{

    public static class  UHFHelper
    {
        /// <summary>
        /// 解析数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string ByteArrayToHexString(this byte[] data)
        {
            StringBuilder sb = new(data.Length * 3);
            foreach (byte b in data)
                sb.Append(Convert.ToString(b, 16).PadLeft(2, '0'));
            return sb.ToString().ToUpper();

        }
    }

    public class Resultoutput
    {
        public bool Res { get; set; }

        public string? ResMes { get; set; }  
    }


    public class DKoutput
    {
        /// <summary>
        /// 刷卡机地址
        /// </summary>
        public string? Address { get; set; }
        /// <summary>
        /// 刷卡器数据
        /// </summary>
        public string? ResData { get; set; }
    }

    /// <summary>
    /// 蜂鸣开关
    /// </summary>

    public enum Beep
    {
        open=0,
        close=1,
    }
    /// <summary>
    /// CRC算法
    /// </summary>
    public class CRC16
    {
        /// <summary>
        /// CRC算法
        /// </summary>
        /// <param name="pucY"></param>
        /// <returns></returns>
        public static byte[] ToCRC16(byte[] pucY)
        {
            ushort uiCrcValue = 0xFFFF;
            for ( int ucI = 0; ucI < pucY.Length; ucI++)
            {
                uiCrcValue = Convert.ToUInt16(uiCrcValue ^ pucY[ucI]);
                for ( int ucJ = 0; ucJ < 8; ucJ++)
                {
                    if ((uiCrcValue & 0x0001)==1)
                    {
                        uiCrcValue =Convert.ToUInt16((uiCrcValue >> 1) ^ 0x8408);
                    }
                    else
                    {
                        uiCrcValue = Convert.ToUInt16((uiCrcValue >> 1));
                    }
                }
            }
            byte[] tem = new byte[2];
            tem[0] = (byte)uiCrcValue;
            tem[1] = (byte)(uiCrcValue >> 8);
            return tem;
        }
    }
}
