locked Re: Decoding UDP messages from WSJT-X


Bill Somerville
 

On 20/12/2019 12:15, Bill Somerville wrote:
On 20/12/2019 11:51, UR5FCM Kii wrote:
          UInt32 magic = BitConverter.ToUInt32(aByte, offset);
 
            Console.WriteLine(" --> " + magic.ToString());
The magic is 3670785197 what get from bytes 
It's not required so what wrong?

Hi OM,

the network data is in Big Endian byte order but your system is Little Endian (BitConveter.isLittleEndian is True), in that case you must swap the field bytes order before or after conversion. E.g.

using System.Buffers.Binary;
Uint32 magic = BitConverter.ToUint32(aByte, offset);
if (BitConverter.isLittleEndian) {
  magic = BinaryPrimitives.ReverseEndianness(magic);
}
Console.WriteLine(" -->> {0:x}", magic);

73
Bill
G4WJS.

Hi again OM,

note that for floating point fields it is slightly more complex. You could extract the bytes into a byte array and reverse that before using BitConverter:

using System;

double ExtractDouble(byte[] aByte, ref int offset) {
  temp = new byte[sizeof(double)];
  Buffer.BlockCopy(aByte, offset, temp, 0, sizeof(double));
  offset = offset + sizeof(double);
  if (BitConveter.isLittleEndian) {
    Array.Reverse(temp);
  }
  return BitConveter.ToDouble(temp, 0);
}

73
Bill
G4WJS.

Join main@WSJTX.groups.io to automatically receive all group messages.