I was wondering if anyone here knows an efficient way to cast an integer to a byte[4]? I'm trying to write an int into MemoryStream, and this thing wants me to give it bytes
You can use BitConverter.GetBytes
if you want to convert a primitive type to its byte representation. Just remember to make sure the endianness is correct for your scenario.
Use a BinaryWriter (constructed with your memory stream); it has a write method that takes an Int32.
BinaryWriter bw = new BinaryWriter(someStream);
bw.Write(intValue);
bw.Write((Int32)1);
// ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With