- ·上一篇教育:word如何加满行下划线,下划线,加满
- ·下一篇教育:word如何只把字下面加粗,只把,加粗
word如何转换成二进制,转换成
1.文字怎么转换成二进制数
代码 /// ///二进制数据转换为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"),"测试成功"); }。
2.文字怎么转换成二进制数
代码 /// ///二进制数据转换为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"),"测试成功"); }。
3.怎样把文件转换成二进制形式的文本文件
好吧你不需要工具的话那我直接用C#的源代码写一下。
public void write010File(string filepath) { FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read); StreamWriter sw = new StreamWriter("out.txt"); byte[] buffer = new byte[2048]; int word = 0; while ((word = fs.Read(buffer, 0, buffer.Length)) > 0) { //这里将读入内存的数据使用二进制写出 for (int i = 0; i < word; i++) { sw.Write(Convert.ToString(buffer[i], 2)); } } sw.Close(); fs.Close(); }将文件路径传入就行了,他会转换为二进制然后输出到out.txt中。
4.如何将文本文件以二进制的方式打开
文件文件(以文本方式写的),最好以文本方式读。
二进制文件(以二进制方式写的),最好以二进制方式读。不然可能会不正确。
在DFM文件与XML文件互转中,用到的dfm文件必须为文本格式,如果是二进制格式, 处理就会出错。 但是在处理中如何判断dfm是二进制文件, 而且再将二进制文件转为文本格式呢. --- dfm文件二进制格式时, 其文件会加一个文件头, 其中前3个字节来标识其为二进制, 这三个字节分别为:$FF, $0A, $00. 因为这三个字节在文本类型的文件中是不可能存在的,所以可以判断这3个字节就可以了。
function IsBinDfm(const ADfmFileName: string): Boolean; Var mBinStream:TMemoryStream; mBuff : array [0..2] of byte; begin mBinStream := TMemoryStream.Create; try mBinStream.LoadFromFile(ADfmFileName); mBinStream.Read(mBuff, 3); //前三字节: $FF, $0A, $00 if (mBuff[0] = $FF) and (mBuff[1] = $0A) and (mBuff[2]= $00) then Result := True else Result := False; finally mBinStream.Free; end; end; 判断出来后, 再将二进制转为文本格式就容易了.Delphi提供了ObjectResourceToText函数.写法如下: procedure DfmBin2Txt(ADfmFileName: string); Var inFileStream: TMemoryStream; outFileStream: TFileStream; begin inFileStream := TMemoryStream.Create; inFileStream.LoadFromFile(ADfmFileName); try outFileStream := TFileStream.Create(ADfmFileName, fmCreate); try try inFileStream.Seek(0, soFromBeginning); ObjectResourceToText(inFileStream, outFileStream); except Raise Exception.Create('This dfm is bin, error on trans bin to txt.'); end; finally outFileStream.Free; end; finally inFileStream.Free; end; end;。