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

namespace ProjectManagementSystem.Device.Function
{
    internal class Tools
    {
        public static string Byte2HexString(byte[] bytes)
        {
            //return BitConverter.ToString(bytes);
            //return string.Join(" ", bytes.Select(d => d.ToString("X2")));

            return GetBytesString(bytes, 0, bytes.Length);
        }

        public static string GetBytesString(byte[] value, int startIndex, int length)
        {
            int num = length * 3;
            char[] array = new char[num];
            int num2 = startIndex;
            for (int i = 0; i < num; i += 3)
            {
                byte b = value[num2++];
                array[i] = GetHexValue((int)(b / 16));
                array[i + 1] = GetHexValue((int)(b % 16));
                array[i + 2] = ' ';
            }
            return new string(array, 0, array.Length - 1);
        }

        private static char GetHexValue(int i)
        {
            if (i < 10)
            {
                return (char)(i + 48);
            }
            return (char)(i - 10 + 65);
        }

        public static string GetBytesString(byte[] bytes, int index, int count, string sep)
        {
            return string.Join(sep, bytes.Skip(index).Take(count).Select(b => b.ToString("X2")));
        }

        public static byte XOR_Check(byte[] buffer, int count, int index = 0)
        {
            byte b = 0;
            for (int i = 0; i < count; i++)
            {
                b ^= buffer[index + i];
            }
            return b;
        }
    }
}

