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

namespace ToolKitlib
{
    #region 转换工具包【ToolKit】

    /// <summary>
    /// 转换工具包
    /// </summary>
    internal class ToolKit
    {
        /// <summary>
        /// 16进制转字符串(十进制)**
        /// </summary>
        /// <param name="hexStr"></param>
        /// <returns></returns>
        public static string hexStr2Str(String hexStr)
        {
            String str = "0123456789ABCDEF";
            char[] hexs = hexStr.ToCharArray();
            byte[] bytes = new byte[hexStr.Length / 2];
            int n;

            for (int i = 0; i < bytes.Length; i++)
            {
                n = str.IndexOf(hexs[2 * i]) * 16;
                n += str.IndexOf(hexs[2 * i + 1]);
                bytes[i] = (byte)(n & 0xff);
            }
            StringBuilder stringBuilder = new StringBuilder("");
            for (int i = 0; i < bytes.Length; i++)
            {
                stringBuilder.Append(bytes[i]);
            }
            return stringBuilder.ToString();
        }

        /// <summary>
        /// 16进制转ASCII码**
        /// </summary>
        /// <param name="hexStr">16进制字符</param>
        /// <returns></returns>
        public static StringBuilder hexToASCII(string hexStr)
        {
            String str = "0123456789ABCDEF";
            char[] hexs = hexStr.ToCharArray();
            byte[] bytes = new byte[hexStr.Length / 2];
            int n;

            for (int i = 0; i < bytes.Length; i++)
            {
                n = str.IndexOf(hexs[2 * i]) * 16;
                n += str.IndexOf(hexs[2 * i + 1]);
                bytes[i] = (byte)(n & 0xff);
            }
            StringBuilder VersionNumber = new StringBuilder("");
            for (int i = 0; i < bytes.Length; i++)
            {
                VersionNumber.Append((char)bytes[i]);
            }
            return VersionNumber;
        }

        /// <summary>
        /// 获取高四位**
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static int getHeight4(byte data)
        {
            int height;
            height = ((data & 0xf0) >> 4);
            return height;
        }

        /// <summary>
        /// 获取低四位**
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static int getLow4(byte data)
        {
            int low;
            low = (data & 0x0f);
            return low;
        }

        /// <summary>
        /// byte数组转string
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string byteArrayToString(byte[] data, Encoding encoding)
        {
            return encoding.GetString(data);
        }

        /// <summary>
        /// 将列表 转为byte[]
        /// </summary>
        /// <param name="matrix"></param>
        /// <returns></returns>
        public static byte[] listToBytes(List<byte> matrix)
        {
            if (matrix == null)
            {
                return new byte[0];
            }
            using (MemoryStream stream = new MemoryStream())
            {
                BinaryWriter bw = new BinaryWriter(stream);
                foreach (var item in matrix)
                {
                    bw.Write(item);
                }
                return stream.ToArray();
            }

        }

        /// <summary>
        /// string转 byte数组
        /// </summary>
        /// <param name="data"></param>
        /// <param name="fromBase">传入数据进制</param>
        /// <returns></returns>
        public static byte[] stringToByteArray(string data, int fromBase)
        {
            if (data.Length < 2 || data.Length % 2 != 0) return null;
            byte[] bytes = new byte[(data.Length) / 2];
            for (int i = 0; i < data.Length; i++)
            {
                if (i != 0 && i % 2 != 0)
                {
                    ushort a = (ushort)Convert.ToInt16((data[i - 1].ToString() + data[i].ToString()).ToString(), fromBase);

                    bytes[(i - 1) / 2] = (byte)(a);
                }
            }

            return bytes;
        }

        /// <summary>
        /// string转byte数组--针对校验位
        /// </summary>
        /// <param name="str"></param>
        /// <returns>[0]--高位;[1]地位--</returns>
        public static byte[] stringToByteArray(string str)
        {
            //var a = "332A";
            ushort u = (ushort)Convert.ToInt16(str, 16);//0x332A
            byte g = (byte)(u / 256);//0x33 高位
            byte d = (byte)(u % 256);//0x2A 地位
            return new byte[] { g, d };
        }

        /// <summary>
        /// byte数组转16进制字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string byteArrayToHexString(byte[] data)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                builder.Append(string.Format("{0:X2} ", data[i]));
            }
            return builder.ToString().Trim();
        }

        /// <summary>
        /// byte数组转16进制字符串--接收
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string byteArrayToHexString(byte[] data, int length = 0)
        {
            int dataLength = 0;
            if (length != 0) dataLength = length;
            else dataLength = data.Length;

            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < dataLength; i++)
            {
                builder.Append(string.Format("{0:X2} ", data[i]));
            }
            return builder.ToString().Trim();
        }

        /// <summary>
        /// 16进制字符串转byte数组
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] hexStringToByteArray(string data)
        {
            string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            byte[] returnBytes = new byte[chars.Length];
            //逐个字符变为16进制字节数据
            for (int i = 0; i < chars.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(chars[i], 16);
            }
            return returnBytes;
        }

        /// <summary>
        /// byte数组转10进制字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string byteArrayToDecString(byte[] data)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                builder.Append(data[i] + " ");
            }
            return builder.ToString().Trim();
        }

        /// <summary>
        /// 10进制字符串转byte数组
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] decStringToByteArray(string data)
        {
            string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            byte[] returnBytes = new byte[chars.Length];
            //逐个字符变为10进制字节数据
            for (int i = 0; i < chars.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(chars[i], 10);
            }
            return returnBytes;
        }

        /// <summary>
        /// byte数组转八进制字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string byteArrayToOtcString(byte[] data)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                builder.Append(Convert.ToString(data[i], 8) + " ");
            }
            return builder.ToString().Trim();
        }

        /// <summary>
        /// 八进制字符串转byte数组
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] otcStringToByteArray(string data)
        {
            string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            byte[] returnBytes = new byte[chars.Length];
            //逐个字符变为8进制字节数据
            for (int i = 0; i < chars.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(chars[i], 8);
            }
            return returnBytes;
        }

        /// <summary>
        /// 二进制字符串转byte数组
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] binStringToByteArray(string data)
        {
            string[] chars = data.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            byte[] returnBytes = new byte[chars.Length];
            //逐个字符变为2进制字节数据
            for (int i = 0; i < chars.Length; i++)
            {
                returnBytes[i] = Convert.ToByte(chars[i], 2);
            }
            return returnBytes;
        }

        /// <summary>
        /// byte数组转二进制字符串
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string byteArrayToBinString(byte[] data)
        {
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                builder.Append(Convert.ToString(data[i], 2) + " ");
            }
            return builder.ToString().Trim();
        }

        /// <summary>
        /// 每8bit高低位互换
        /// </summary>
        /// <param name="j">起始</param>
        /// <param name="k">终止</param>
        /// <param name="message">需要转换的16进制数据</param>
        /// <returns>新的16进制数据</returns>
        public static string interconvert(int j, int k, string message, bool exchange = true)
        {
            StringBuilder parityBitCS = new StringBuilder("");//互换后的值

            string[] strings = new string[4] { message[0].ToString() + message[1].ToString(), message[2].ToString() + message[3].ToString(), message[4].ToString() + message[5].ToString(), message[6].ToString() + message[7].ToString() };
            for (int i = strings.Length - 1; i >= 0; i--)
            {
                parityBitCS.Append(strings[i].ToString());
            }
            return Convert.ToInt32(parityBitCS.ToString(), 16).ToString();
        }

        /// <summary>
        /// byte[]直接转Float**
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static float byteToFloat(string message)
        {
            byte[] bFxianOrg = new byte[4];
            var a = Encoding.Default.GetBytes(message);
            StringBuilder zz = new StringBuilder("");
            StringBuilder _zz = new StringBuilder("");
            string[] s = new string[4];
            int x = 0;
            for (int i = 0; i < a.Length; i++)
            {
                if (x != 0 && i % 2 == 0)
                {
                    zz.Append(int.Parse(hexStr2Str(_zz.ToString())).ToString()).Append("-");
                    _zz.Clear();
                    x = 0;
                }
                _zz.Append(((char)a[i]).ToString());
                if (i == a.Length - 1)
                {
                    zz.Append(int.Parse(hexStr2Str(_zz.ToString())).ToString()).Append("-");
                    _zz.Clear();
                }
                x++;
            }
            s = zz.ToString().Split('-');
            byte a0 = byte.Parse(s[0].ToString());
            byte a1 = byte.Parse(s[1].ToString());
            byte a2 = byte.Parse(s[2].ToString());
            byte a3 = byte.Parse(s[3].ToString());
            bFxianOrg = new byte[4] { a0, a1, a2, a3 };
            var b = BitConverter.ToSingle(bFxianOrg, 0);
            return b;
        }

        /// <summary>
        /// string[]转string**
        /// </summary>
        /// <param name="vs"></param>
        /// <returns></returns>
        public static string strsTostring(string[] vs)
        {
            return String.Join("", vs);//引号中可加字符串分隔符
        }

        
    }
    #endregion
}
