Hi
How do I copy a double, int, bool or other built-in type to a byte array in C#?
I need to do it to use the FileStream.Write()
method.
Hi
How do I copy a double, int, bool or other built-in type to a byte array in C#?
I need to do it to use the FileStream.Write()
method.
BitConverter.GetBytes()
can convert primitive types to byte arrays.
Instead of converting each value to a byte array, you can use a BinaryWriter
to write the values to the file stream.
Example:
using (BinaryWriter writer = new BinaryWriter(fileStream)) {
writer.Write(1);
writer.Write(1.0);
writer.Write(true);
writer.Write("Hello");
}