开发者

How can I write a binary array to a file in C#?

I have to write the below binary array into a file:

byte[] data = new byte[] { 0x55, 0xAA, 0x02};

I开发者_开发知识库 want to put the exact data into the file (55,AA,02). Please let me know how to do it.


You can use the Stream.Write(byte[] buffer) overload.

And even easier,

   System.IO.File.WriteAllBytes("fileName", data);


Please try the following:

FileStream fs = new FileStream(Application.StartupPath + "\\data.bin", FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
byte[] data = new byte[] { 0x55, 0xAA, 0x02 };
bw.Write(data);
bw.Close();
fs.Close();


Iirc you can use

string content = BitConverter.ToString(data);

to retrieve a string containing the content and then write that string to the File you want.


You can use File.WriteAllBytes(string path, byte[] bytes).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜