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

namespace SERVER
{
    /// <summary>
    /// 文件帮助类
    /// </summary>
    public static class ConvertFile
    {
        /// <summary>
        /// 转换二进制(Bin文件)
        /// </summary>
        /// <param name="Path"></param>
        /// <returns></returns>
        public static string ReadDat(Stream Path)
        {
            try
            {
                //FileStream myStream = new FileStream(Path, FileMode.Open, FileAccess.Read);
                //使用FileStream对象实例化BinaryReader二进制写入流对象
                BinaryReader myReader = new BinaryReader(Path);
                if (myReader.PeekChar() != -1)
                {
                    //以二进制方式读取文件中的内容
                    return Convert.ToString(myReader.ReadString());
                }
                //关闭当前二进制读取流
                myReader.Close();
                //关闭当前文件流
                Path.Close();
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }

        }



        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="stream"></param>
        /// <returns></returns>
        public static string StreamToString(Stream stream)
        {
            stream.Position = 0;
            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }

        /// <summary>
        /// 将字符串转成二进制
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string bianma(string s)
        {
            byte[] data = Encoding.Unicode.GetBytes(s);
            StringBuilder result = new StringBuilder(data.Length * 8);

            foreach (byte b in data)
            {
                result.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
            }
            return result.ToString();
        }


        /// <summary>
        /// 对hex文件处理
        /// </summary>
        /// <param name="RE"></param>
        public static string hexData(string RE)
        {
            //存储总数据
            string data = "";
            //获取每行
            var setd = RE.Split(":");

            for (int i = 0; i < setd.Length; i++)
            {
                if (setd[i] != "")

                {
                    if (setd[i].Substring(6, 2) == "00")
                    {


                        int hand = int.Parse(setd[i].Substring(0, 2)) * 2;
                        if(int.Parse(setd[i].Substring(0, 2)) == 10) 
                        {
                            hand = 32;
                        }
                        data += setd[i].Substring(8, hand);
                    }
                    //for (int j = 0; j < int.Parse(setd[i].Substring(0,1)); j++)
                    //{
                    //    setd[i].Substring(2 * j + 9, 2);
                    //}
                }
            }
            return data;
        }
    }
}
