none
BYTE Swap Endianness RRS feed

  • Question

  • Hello

    I have a ethernt server that receives data from a PowerPC, and that architecure is Big Endian, and I'm running off a Intel Win7 PC, so I need to convert to little endian.

    I receive into a Bufer of BYTE type, so how should I do this for an array of bytes?

    Ideas?

    Sunday, February 15, 2015 8:24 PM

Answers

  • The easiest way to do this is to create your own array of just the number, reverse it, then use BitConverter to convert it back to a number.

    For example, you have the bytes of: 0102030405 and you have a 32-bit integer that you will read starting at an offset of 1 of the start of the array (you want the number 0x05040302 or 84148994).

    byte[] sourceArray = new byte[] { 1, 2, 3, 4, 5 };

    byte[] numArray = new byte[4];

    Buffer.BlockCopy(sourceArray, 1, numArray, 0, 4);

    Array.Reverse(numArray); // Reverses the endianness.

    int result = BitConverter.ToInt32(numArray, 0);

    Sunday, February 15, 2015 8:53 PM