2016-03-08 24 views
1

我试图通过UDP发送ints,uint和float,所有32bit的混合。将结构转换为字节数组,转换为大端并通过UDP发送

我的问题是我似乎在浮游物中发送垃圾。我发送的所有内容必须采用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周围的字节,这似乎有点长,但它似乎适用于整数和提示。它通过字节数组遍历,复制每个字节,扭转它,然后把返回到一个新的数组:

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的,在的uint和int为以正确的字节顺序发送,但花车持有垃圾。 43:0a:33:33的值138.2

回答

0

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

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; 

}

相关问题