How does this program work?

That’s because %d expects an int but you’ve provided a float. Use %e/%f/%g to print the float. On why 0 is printed: The floating point number is converted to double before sending to printf. The number 1234.5 in double representation in little endian is 00 00 00 00 00 4A 93 40 A %d consumes … Read more

Why is network-byte-order defined to be big-endian? [closed]

RFC1700 stated it must be so. (and defined network byte order as big-endian). The convention in the documentation of Internet Protocols is to express numbers in decimal and to picture data in “big-endian” order [COHEN]. That is, fields are described left to right, with the most significant octet on the left and the least significant … Read more

Detecting Endianness

As stated earlier, the only “real” way to detect Big Endian is to use runtime tests. However, sometimes, a macro might be preferred. Unfortunately, I’ve not found a single “test” to detect this situation, rather a collection of them. For example, GCC recommends : __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ . However, this only works with latest versions, … Read more

BinaryWriter Endian issue

You can use my EndianBinaryWriter in MiscUtil. That lets you specify the endianness you want. There’s also EndianBinaryReader and EndianBitConverter. EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Big, stream); writer.Write(…); It doesn’t derive from BinaryWriter, for reasons given in a blog post.

How do I convert an array of floats to a byte[] and back?

If you’re looking for performance then you could use Buffer.BlockCopy. Nice and simple, and probably about as fast as you’ll get in managed code. var floatArray1 = new float[] { 123.45f, 123f, 45f, 1.2f, 34.5f }; // create a byte array and copy the floats into it… var byteArray = new byte[floatArray1.Length * 4]; Buffer.BlockCopy(floatArray1, … Read more