文字怎么转换成二进制数

2020-09-24 科技 151阅读
代码 /// ///二进制数据转换为word文件 /// /// 二进制数据 /// word文件名 /// word保存的相对路径 public string ByteConvertWord(byte[] data, string fileName) { string savePath =@"\SystemWord\"+FormatNowTime(2)+@"\"; if (!System.IO.Directory.Exists(GetPath() + savePath)) { Directory.CreateDirectory(GetPath() + savePath); } savePath += fileName + ".doc"; string filePath = GetPath() + savePath; FileStream fs; if (System.IO.File.Exists(filePath)) { fs = new FileStream(filePath,FileMode.Truncate); } else { fs = new FileStream(filePath,FileMode.CreateNew); } BinaryWriter br = new BinaryWriter(fs); br.Write(data, 0, data.Length); br.Close(); fs.Close(); return savePath; } /// /// word文件转换二进制数据(用于保存数据库) /// /// word文件路径 /// 二进制 private byte[] wordConvertByte(string wordPath) { byte[] bytContent = null; System.IO.FileStream fs = null; System.IO.BinaryReader br = null; try { fs = new FileStream(wordPath,System.IO.FileMode.Open); } catch { } br = new BinaryReader((Stream)fs); bytContent = br.ReadBytes((Int32)fs.Length); return bytContent; } /// ///项目所在目录 /// /// public string GetPath() { return Application.StartupPath; } /// ///格式化当前时间: /// 1:yyMMddHHmmss; 2:yyyy-MM\dd\ /// /// public string FormatNowTime(int num) { if (num == 1) { returnDateTime.Now.ToString("yyMMddHHmmss"); } else if (num == 2) { returnDateTime.Now.ToString("yyyy-MM") + @"\" + DateTime.Now.Day; } return ""; } //测试方法 private void button1_Click(object sender,EventArgs e) { string newWord = ByteConvertWord(wordConvertByte(@"D:\测试文件.doc"),"测试成功"); }
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com