using System.IO; int num = 123456789; using (FileStream stream = new FileStream("data.bin", FileMode.Create)) { using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8)) { writer.WriteBE(num); } }
using System.IO; float num = 3.14f; using (FileStream stream = new FileStream("data.bin", FileMode.Create)) { using (BinaryWriter writer = new BinaryWriter(stream, Encoding.UTF8)) { byte[] bytes = BitConverter.GetBytes(num); Array.Reverse(bytes); writer.Write(bytes); } }In this example, we create a new float variable `num` with the value of 3.14. We then create a new file stream and binary writer to write to the file. To write the float in big-endian format, we first convert it to bytes using BitConverter.GetBytes method. Then, we reverse the array to swap the positions of the most significant byte and least significant byte. We finally write the bytes to the file using the Write method.