using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace SERVER { /// /// 文件帮助类 /// public static class ConvertFile { /// /// 转换二进制(Bin文件) /// /// /// 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; } } /// /// 读取文件 /// /// /// public static string StreamToString(Stream stream) { stream.Position = 0; using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { return reader.ReadToEnd(); } } /// /// 将字符串转成二进制 /// /// /// 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(); } /// /// 对hex文件处理 /// /// 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; } } }