Converting a structure to byte array, converting to big endian and sending over UDP
<p>I'm trying to send a mixture of ints, uints and floats, all 32bit, over UDP. </p>

<p>My problem is I seem to be sending rubbish in the floats. Everything I send has to be in the Big Endian Format.</p>

<p>Any help would be appreciated</p>

<p>Thanks</p>

<p>I have a message definition:</p>

<pre><code>message.param1         = 0x6666BBBB;     //uint
message.param2         = 0x6666BBBB;     //uint
message.param3         = 5;             //uint
message.param4         = 112;           //uint
message.param5         = 0x6666BBBB;     //uint
message.param6         = 0x6666BBBB;     //uint
message.param7         = 0x6666BBBB;     //uint
message.param7         = Convert.ToInt32(textbox.Text);  //int, holds 1
message.param8         = float.Parse(textbox.Text);    //float, holds 138.2
message.param9         = float.Parse(textbox.Text);    //float, holds 20.0
</code></pre>

<p>I then convert to a byte array using:</p>

<pre><code>byte[] packet = convert.StructureToByteArray(message);

public byte[] StructureToByteArray(object obj)
    { 
        int len = Marshal.SizeOf(obj);
        byte[] arr = new byte[len];

        IntPtr ptr = Marshal.AllocHGlobal(len);
        Marshal.StructureToPtr(obj, ptr, true);
        Marshal.Copy(ptr, arr, 0, len);
        Marshal.FreeHGlobal(ptr);

        return arr;
    }
</code></pre>

<p>then I swap the bytes around to Big Endian, this seems a bit long winded but it seems to work for the ints and uints. It iterates through the byte array, copying each byte, reversing it and then puts back into a new array:</p>

<pre><code>byte[] ConvertedPacket = convert.StructureToBigEndien(packet);

public byte[] StructureToBigEndien(byte[] packet)
    {

         // create newarray to hold converted data in;
        byte[] newArray = new byte[packet.Length];

         // number of bytes in 32 bits
        int NumberOfBytes = 4;

        // swap the endianess around to big endian
        for (int counter1 = 0; counter1 &lt;= packet.Length - NumberOfBytes; counter1++)
        {
            // create new byte to hold converted byte
            byte[] bits = new byte[4];

            // count to 4, each time copying one byte at a time
            for (int counter2 = 0; counter2 &lt;= 3; counter2++)
            {
                Array.Copy(packet, counter1, bits, counter2, 1);

                // increment the counter so it remembers where to start the next copy
                counter1++;
            }

            // now for the copying
                // take one away as the counter hits again before hitting this statement  
                counter1 = counter1 - 1;

                // reverse the bytes in our new array, now this holds the correct endianness
                Array.Reverse(bits);

                // now take 3 away, this is because we need to 
                // insert the byte at the beginning of the new byte
                // array
                counter1 = counter1 - 3;

                // coppy data across
                bits.CopyTo(newArray, counter1);

                // reset counter to what it was so it knows how many bytes are left to go in the packet
                counter1 = counter1 + 3;
        }

        return newArray;
    }
</code></pre>

<p>And then I send</p>

<pre><code>socket.SendTo(ConvertedPacket, endPoint);
</code></pre>

<p>When I check wireshark, the uints and ints are sent in the correct endianness but the floats hold garbage. 43:0a:33:33 for the value 138.2</p>