开发者

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

开发者 https://www.devze.com 2023-02-01 02:36 出处:网络
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 d

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

精彩评论

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