Tag: binaryfiles

使用System.IO.BinaryWriter编写字符串与char数组的区别

我正在使用C#将文本写入二进制文件,并查看写入字符串和字符数组之间的数量差异。 我正在使用System.IO.BinaryWriter并在写入时观察BinaryWriter.BaseStream.Length。 这些是我的结果: using(BinaryWriter bw = new BinaryWriter(File.Open(“data.dat”), Encoding.ASCII)) { string value = “Foo”; // Writes 4 bytes bw.Write(value); // Writes 3 bytes bw.Write(value.ToCharArray()); } 我不明白为什么当我只写3个ASCII字符时,字符串重载会写入4个字节。 有谁能解释一下?

C#加载二进制文件

请告诉我最佳/快速的方法: 1)将非常小的二进制文件加载到内存中。 例如图标; 2)加载/读取大小为512Mb +的非常大的二进制文件。 3)当你不想考虑大小/速度但必须做的事情时你的共同选择:将所有字节读入内存? 谢谢!!! PS抱歉,这可能是一个微不足道的问题。 请不要关闭;) PS2。 Java模拟问题的镜像 ;

在c#中读取复合文档

我正在开始一个需要在c#中读取outlook msg文件的项目。 我有复合文档的规范,但在c#中读取它们时遇到问题。 任何指针都将非常感激。 谢谢。

使用BinaryReader读取“little-endian UTF-16编码中的字符串”

我遵循以下文件格式的规范: https : //github.com/rouault/dump_gdbtable/wiki/FGDB-Spec utf16: string in little-endian UTF-16 encoding 我怎么读这个? 我尝试了BinaryReader.ReadString()但它返回的内容如下: “\0e\0y\0w\0o\0r\0d\0\0 \0\0\0\0\rP\0a\0r\0a\0m\0e\0t\0e\0r\0N\0a\0m\0e\0\0 \0\0\0\0\fC\0o\0n\0f\0i\0g\0S\0t\0r\0” 这绝对不对。 从规格: ubyte: number of UTF-16 characters (not bytes) of the name of the field utf16: name of the field ubyte: number of UTF-16 characters (not bytes) of the alias of the field. Might be 0 utf16: alias of the […]

二进制文件到字符串

我正在尝试将二进制文件(例如可执行文件)读入字符串,然后将其写回 FileStream fs = new FileStream(“C:\\tvin.exe”, FileMode.Open); BinaryReader br = new BinaryReader(fs); byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length)); System.Text.Encoding enc = System.Text.Encoding.ASCII; string myString = enc.GetString(bin); fs.Close(); br.Close(); System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); byte[] rebin = encoding.GetBytes(myString); FileStream fs2 = new FileStream(“C:\\tvout.exe”, FileMode.Create); BinaryWriter bw = new BinaryWriter(fs2); bw.Write(rebin); fs2.Close(); bw.Close(); 这不起作用(结果具有完全相同的字节大小但无法运行) 如果我做bw.Write(bin)结果没问题,但我必须将它保存为字符串