Frozen neurons: BitConverter and endianess

Hi everyone,

I am stuck with datatype representation in a byte array. I am using the serial buffer class available on codeshare to retrieve some data coming from a serial port. the data are stored in the buffer variable:


 byte[] bp = new byte[2];
 bp[0] = buffer[startIndex + 4];
 bp[1] = buffer[startIndex + 5];
 var payloadSize = BitConverter.ToUInt16(bp, 0);
 string deb = "";
 Debug.Print("startIndex= " + startIndex + " endindex= " + endIndex);
 Debug.Print("payload 1 = " + payloadSize + " bp0=" + bp[0] + " bp1=" + bp[1]);  //a reprendre
 var payloadSize2 = BitConverter.ToUInt16(buffer, startIndex + 4);
 Debug.Print("payload 2 = " + payloadSize2 +" bp0=" +buffer[startIndex+4]+ " bp1="+buffer[startIndex+5]);
 foreach (byte b in buffer)
         {
         deb += " " + BitConverterCustom.FromByteToHex(b);
         }
Debug.Print(deb);

Here’s the output console
startIndex= 11 endindex= 418
payload 1 = 32 bp0=32 bp1=0
payload 2 = 8192 bp0=32 bp1=0
09 00 00 00 A8 0D D8 08 52 A5 33 FF 5A 06 00 20 00 80 45 EA DB EF 82 70 BD 3A E9 E1 3C CF D2 35 3F B6 B1 10 3B CD 85 10 3B 1C 72 3A 3E 11 01 00 00 8D A7 33 FF 5A 08 00 48 00 80 45 EA DB 4A 24 A8 C0 F8 D9 C3 41 92 8A A7 44 00 00 FA 43 00 00 FA 43 00 00 FA 43 A6 4D 2B 0B 71 C4 48 40 CF E3 A2 07 51 FA 2B 40 00 00 00 00 80 84 1E C1 9E 6F 46 42 00 00 7A 44 00 00 7A 44 00 00 7A 44 11 01 00 00 1B 05 33 FF 5A 09 00 2E 00 E8 0A EA DB 99 0A A0 40 00 00 00 00 00 00 00 00 A6 57 AD 3C 00 00 00 00 00 00 00 00 F8 49 9D 3B 00 00 00 00 00 00 00 00 07 7E A8 3B 09 00 41 DC 33 FF 5A 0D 00 2C 00 80 45 EA DB 80 00 00 00 A8 0D D8 08 1B 2F DD BC C1 CA A1 3D 4E 62 90 3E B9 1E 25 3F B9 1E 25 3F B9 1E 25 3F 01 64 70 43 7F C2 2C 42 D5 D4 33 FF 5A 06 00 20 00 C0 52 ED DB EA 67 70 BD 65 19 E2 3C 35 E8 35 3F E6 B0 10 3B 81 86 10 3B 46 75 3A 3E 11 01 00 00 2F 4B 33 FF 5A 08 00 48 00 C0 52 ED DB 5A 42 A8 C0 2A D8 C3 41 A8 8A A7 44 00 00 FA 43 00 00 FA 43 00 00 FA 43 26 B7 CD BB 70 C4 48 40 AC B8 18 EE 59 FA 2B 40 00 00 00 00 80 84 1E C1 9E 6F 46 42 00 00 7A 44 00 00 7A 44 00 00 7A 44 11 01 00 00 08 62 33 FF 5A 09 00 2E 00 28 18 ED DB 99 0A A0 40 00 00 00 00 00 00 00 00 CB BF B2 3C 00 00 00 00 00 00 00 00 F3 52 A9 BB 00 00 00 00 00 00 00 00 E6 3E B5 3B 09 00 38 19 33

SYNC1 SYNC2 Mess ID Mess Class LEN Data CRC ETX
0xFF 0x5A 0x-- 0x-- 0x-- 0x-- 0x–… 0x-- 0x-- 0x33

Length of Data Section is given on two bytes. In this message: buffer[15]=0x20 buffer [16]=0x00 resulting in a 32 bytes of data.

Why the second attempt to compute payload is wrong?

Is there any one else that observe weird behavior of the bit converter class?

@ leforban - Could you make a self contained example with the data pre-populated?

Here it is:


using System;
using Microsoft.SPOT;

namespace testBitconverter
{
    public class Program
    {
        public static void Main()
        {
            int startIndex=11;
            
            Debug.Print(Resources.GetString(Resources.StringResources.String1));
            var buffer = new byte[] {0x09,0x00,0x00,0x00,0xA8,0x0D,0xD8,0x08,0x52,0xA5,0x33,0xFF,0x5A,0x06,0x00,0x20,0x00,0x80,0x45,0xEA,0xDB,0xEF,0x82,0x70,0xBD,0x3A,0xE9,0xE1,0x3C,0xCF,0xD2,0x35,0x3F,0xB6,0xB1,0x10,0x3B,0xCD,0x85,0x10,0x3B,0x1C,0x72,0x3A,0x3E,0x11,0x01,0x00,0x00,0x8D,0xA7,0x33,0xFF,0x5A,0x08,0x00,0x48,0x00,0x80,0x45,0xEA,0xDB,0x4A,0x24,0xA8,0xC0,0xF8,0xD9,0xC3,0x41,0x92,0x8A,0xA7,0x44,0x00,0x00,0xFA,0x43,0x00,0x00,0xFA,0x43,0x00,0x00,0xFA,0x43,0xA6,0x4D,0x2B,0x0B,0x71,0xC4,0x48,0x40,0xCF,0xE3,0xA2,0x07,0x51,0xFA,0x2B,0x40,0x00,0x00,0x00,0x00,0x80,0x84,0x1E,0xC1,0x9E,0x6F,0x46,0x42,0x00,0x00,0x7A,0x44,0x00,0x00,0x7A,0x44,0x00,0x00,0x7A,0x44,0x11,0x01,0x00,0x00,0x1B,0x05,0x33,0xFF,0x5A,0x09,0x00,0x2E,0x00,0xE8,0x0A,0xEA,0xDB,0x99,0x0A,0xA0,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xA6,0x57,0xAD,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x49,0x9D,0x3B,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x7E,0xA8,0x3B,0x09,0x00,0x41,0xDC,0x33,0xFF,0x5A,0x0D,0x00,0x2C,0x00,0x80,0x45,0xEA,0xDB,0x80,0x00,0x00,0x00,0xA8,0x0D,0xD8,0x08,0x1B,0x2F,0xDD,0xBC,0xC1,0xCA,0xA1,0x3D,0x4E,0x62,0x90,0x3E,0xB9,0x1E,0x25,0x3F,0xB9,0x1E,0x25,0x3F,0xB9,0x1E,0x25,0x3F,0x01,0x64,0x70,0x43,0x7F,0xC2,0x2C,0x42,0xD5,0xD4,0x33,0xFF,0x5A,0x06,0x00,0x20,0x00,0xC0,0x52,0xED,0xDB,0xEA,0x67,0x70,0xBD,0x65,0x19,0xE2,0x3C,0x35,0xE8,0x35,0x3F,0xE6,0xB0,0x10,0x3B,0x81,0x86,0x10,0x3B,0x46,0x75,0x3A,0x3E,0x11,0x01,0x00,0x00,0x2F,0x4B,0x33,0xFF,0x5A,0x08,0x00,0x48,0x00,0xC0,0x52,0xED,0xDB,0x5A,0x42,0xA8,0xC0,0x2A,0xD8,0xC3,0x41,0xA8,0x8A,0xA7,0x44,0x00,0x00,0xFA,0x43,0x00,0x00,0xFA,0x43,0x00,0x00,0xFA,0x43,0x26,0xB7,0xCD,0xBB,0x70,0xC4,0x48,0x40,0xAC,0xB8,0x18,0xEE,0x59,0xFA,0x2B,0x40,0x00,0x00,0x00,0x00,0x80,0x84,0x1E,0xC1,0x9E,0x6F,0x46,0x42,0x00,0x00,0x7A,0x44,0x00,0x00,0x7A,0x44,0x00,0x00,0x7A,0x44,0x11,0x01,0x00,0x00,0x08,0x62,0x33,0xFF,0x5A,0x09,0x00,0x2E,0x00,0x28,0x18,0xED,0xDB,0x99,0x0A,0xA0,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xCB,0xBF};
            byte[] bp= new byte[2];
             bp[0] = buffer[startIndex + 4];
             bp[1] = buffer[startIndex + 5];
            var payloadSize = BitConverter.ToUInt16(bp, 0);
            
            Debug.Print("startIndex= " + startIndex );
            Debug.Print("payload 1 = " + payloadSize + " bp0=" + bp[0] + " bp1=" + bp[1]);  //a reprendre
            var payloadSize2 = BitConverter.ToUInt16(buffer, startIndex + 4);
            Debug.Print("payload 2 = " + payloadSize2 + " bp0=" + buffer[startIndex + 4] + " bp1=" + buffer[startIndex + 5]);
            var payloadSize3 = BitConverter.ToUInt16(new byte[] {buffer[startIndex + 4],buffer[startIndex + 5]},0 );
            Debug.Print("payload 3 = " + payloadSize3 + " bp0=" + buffer[startIndex + 4] + " bp1=" + buffer[startIndex + 5]);
        }
    }
}

The thread ‘’ (0x2) has exited with code 0 (0x0).
Hello World!
startIndex= 11
payload 1 = 32 bp0=32 bp1=0
payload 2 = 8192 bp0=32 bp1=0
payload 3 = 32 bp0=32 bp1=0
The thread ‘’ (0x1) has exited with code 0 (0x0).
The program ‘[2] Micro Framework application: Managed’ has exited with code 0 (0x0).

@ leforban - I think it may have to do with unaligned access because of an issue in BitConverter: Change native BitConverter to not use Reinterpret Cast. by doingnz · Pull Request #395 · NETMF/netmf-interpreter · GitHub.

In your complete example, your start index is 11 and you add 4 to it, so you access at 15. The underlying start of the array is likely aligned, so starting at index 15 won’t work. To test to be sure, set startIndex to an aligned value, say 8, and see if the returned values are the same in all three cases.

1 Like

Indeed creating a byte array aligned solves the problem

Thanks!