当前位置: 动力学知识库 > 问答 > 编程问答 >

.net - 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.

网友答案:

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 the Stream.Write(byte[] buffer) overload.

And even easier,

   System.IO.File.WriteAllBytes("fileName", data);
网友答案:

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

网友答案:

Please try it

     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();
分享给朋友:
您可能感兴趣的文章:
随机阅读: