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

namespace HKCarPlateRecognize_App
{
    public static class ByteUtil
    {
		///右补齐
        public static byte[] PadRight(this IEnumerable<byte> SourceBytes, int TotalLength, byte PaddingByte)
        {
            if (TotalLength <= SourceBytes.Count()) return SourceBytes.ToArray();
            var TargetBytes = new byte[TotalLength];
            for (int i = 0; i < SourceBytes.Count(); i++)
                TargetBytes[i] = SourceBytes.ElementAt(i);
            for (int i = SourceBytes.Count() ; i < TotalLength; i++)
                TargetBytes[i] = PaddingByte;
            return TargetBytes;
        }
		
		///左补齐
        public static byte[] PadLeft(this IEnumerable<byte> SourceBytes, int TotalLength, byte PaddingByte)
        {
            if (TotalLength <= SourceBytes.Count()) return SourceBytes.ToArray();
            var TargetBytes = new byte[TotalLength];
            for (int i = 0; i < TotalLength - SourceBytes.Count(); i++)
                TargetBytes[i] = PaddingByte;
            for (int i = 0; i < SourceBytes.Count(); i++)
                TargetBytes[TotalLength - SourceBytes.Count() + i] = SourceBytes.ElementAt(i);
            return TargetBytes;
        }
		
		///字节数组转为十六进制字符串
        public static string BytesToHexString(this byte[] Bytes)
        {
            var HexString = new StringBuilder();
            for (int i = 0; i < Bytes.Length; i++)
            {
                HexString.Append(Bytes[i].ToString("X").PadLeft(2, '0'));
            }
            return HexString.ToString();
        }
		
		///字节数组转为十六进制字符串
        public static string BytesToHexString(this byte[] Bytes, int Offset, int Length)
        {
            var HexString = "";
            for (int i = Offset; i < (Offset + Length); i++)
            {
                HexString += Bytes[i].ToString("X").PadLeft(2, '0');
            }
            return HexString;
        }
		
		///十六进制字符串转为字节数组
        public static byte[] HexStringToBytes(this string HexStr)
        {
            var NewHexStr = "";
            foreach (char Hex in HexStr)
                if (Uri.IsHexDigit(Hex))
                    NewHexStr += Hex;
            if (NewHexStr.Length % 2 != 0) NewHexStr = NewHexStr.PadLeft(NewHexStr.Length + 1, '0');
            byte[] Buffer = new byte[NewHexStr.Length / 2];
            for (int i = 0; i < NewHexStr.Length; i += 2)
                Buffer[i / 2] = (byte)Convert.ToByte(NewHexStr.Substring(i, 2), 16);
            return Buffer;
        }
		
		///获取字节数组的一部分
        public static byte[] GetPartBytes(this IEnumerable<byte> Bytes, int OffSet, int Length)
        {
            var PartBytes = new byte[Length];
            Array.Copy(Bytes.ToArray(), OffSet, PartBytes, 0, Length);
            return PartBytes;
        }
		
		///获取字节数组的一部分
        public static byte[] GetPartBytes(this IEnumerable<byte> Bytes, int OffSet)
        {
            var PartBytes = new byte[Bytes.Count() - OffSet];
            Array.Copy(Bytes.ToArray(), OffSet, PartBytes, 0, PartBytes.Length);
            return PartBytes;
        }
		
		///比较字节数组内容是否相等
        public static bool EnumerableEquals(this IEnumerable<byte> Bytes, IEnumerable<byte> OtherBytes)
        {
            if (Bytes == null || OtherBytes == null) return false;
            if (Bytes.Count() != OtherBytes.Count()) return false;
            for (int i = 0; i < Bytes.Count(); i++)
            {
                if (Bytes.ElementAt(i) != OtherBytes.ElementAt(i)) return false;
            }
            return true;
        }
    }
}