将结构转换为字节数组,转换为big endian并通过UDP发送

时间:2016-03-08 17:31:43

标签: c# udp bytearray precision endianness

我试图通过UDP发送所有32位的整数,uint和浮点数。

我的问题是我好像在漂浮物中发送垃圾。我发送的所有内容都必须采用Big Endian格式。

任何帮助将不胜感激

由于

我有一个消息定义:

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

然后我使用:

转换为字节数组
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;
    }

然后我将字节交换到Big Endian,这看起来有点长,但它似乎适用于int和uint。它遍历字节数组,复制每个字节,将其反转,然后放回一个新数组:

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 <= 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 <= 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;
    }

然后我发送

socket.SendTo(ConvertedPacket, endPoint);

当我检查wireshark时,uints和int以正确的字节顺序发送,但浮动包含垃圾。 43:0a:33:33,值138.2

2 个答案:

答案 0 :(得分:0)

0x430A3333在IEEE754下为138.2。你为什么说它的垃圾?

答案 1 :(得分:0)

感谢您的澄清,我不得不反转浮点数中的字节以获得正确的字节顺序。

float ReverseFloat(const float inFloat)
{
    float retVal;
    char *floatToConvert = (char*)& inFloat;
    char *returnFloat = (char*)& retVal;

    //swap the bytes into a temporary buffer
    returnFloat[0] = floatToConvert[3];
    returnFloat[1] = floatToConvert[2];
    returnFloat[2] = floatToConvert[1];
    returnFloat[3] = floatToConvert[0];

return retVal;

}