14

So I was using BitConverter.GetBytes() to convert uint32 to byte[], but my the order in the array seems to be backwards. http://msdn.microsoft.com/en-us/library/1184xdy4.aspx (shows that it's byte[] seems to be little endian).

Is there a better way handle this than just using linq to reverse the byte arrays?

Joel Barsotti
  • 3,031
  • 7
  • 35
  • 59
  • I guess that's where I'm confused though since BitConverter.IsLittleEndian returns false, but BitConverter.GetBytes() returns little endian arrays. – Joel Barsotti Aug 03 '10 at 17:07
  • If this is Windows, BitConverter.IsLittleEndian returning false is clearly a bug, since x86 and x86-64 processors are Little Endian. – Powerlord Aug 05 '10 at 15:16

5 Answers5

7

Array.Reverse to change the endianess.

leppie
  • 115,091
  • 17
  • 196
  • 297
6

If you are going to be moving between platform architectures simply reversing the array might be correct on one platform, but fail on a platform that is already using big endian.

You use the IPAddress.HostToNetworkOrder functions, which will ensure that the data is always in network order (big endian).

uint number = 234234233;
uint bigEndian = (uint)IPAddress.HostToNetworkOrder((int)number);
byte[] b = BitConverter.GetBytes(bigEndian);
Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
  • Thank you. It is faster than Array.Reverse. But I had troubles with types while not wrote code like this: var buffer = BitConverter.GetBytes((uint)IPAddress.HostToNetworkOrder((int)i)); – Geograph Jul 21 '18 at 10:43
  • @Geograph, what trouble did you have? It might be worth creating a dedicated question for this, that way you can provide a more complete example and details. – Chris Taylor Jul 21 '18 at 15:47
  • Your original example can't compile with error "CS0266 Cannot implicitly convert type 'long' to 'uint'. An explicit conversion exists (are you missing a cast?)" – Geograph Jul 22 '18 at 16:25
  • @Geograph, I see. Yes for this to work correctly you would need to cast number to int, to get a int sized result back. I will update the sample, thanks. – Chris Taylor Jul 22 '18 at 22:33
5

Microsoft discusses the little-endian / big-endian issue with the GetBytes() method on the documentation page for BitConverter.

C8H10N4O2
  • 1,686
  • 12
  • 8
3

So I figured out a big part of my confusion is related to this: IsLittleEndian field reports false, but it must be Little-Endian?

What I ended up doing was wrapping all the BitConverter calls with call that took an extra parameter to specifiy endianness, then added a function that gets called to check to see if the bytes need to be revered.

public static class BitEndianConverter
{
    public static byte[] GetBytes(bool value, bool littleEndian)
    {
        return ReverseAsNeeded(BitConverter.GetBytes(value), littleEndian);
    }

    public static byte[] GetBytes(char value, bool littleEndian)
    {
        return ReverseAsNeeded(BitConverter.GetBytes(value), littleEndian);
    }
    public static byte[] GetBytes(double value, bool littleEndian)
    {
        return ReverseAsNeeded(BitConverter.GetBytes(value), littleEndian);
    }
    public static byte[] GetBytes(float value, bool littleEndian)
    {
        return ReverseAsNeeded(BitConverter.GetBytes(value), littleEndian);
    }
    public static byte[] GetBytes(int value, bool littleEndian)
    {
        return ReverseAsNeeded(BitConverter.GetBytes(value), littleEndian);
    }
    public static byte[] GetBytes(long value, bool littleEndian)
    {
        return ReverseAsNeeded(BitConverter.GetBytes(value), littleEndian);
    }
    public static byte[] GetBytes(short value, bool littleEndian)
    {
        return ReverseAsNeeded(BitConverter.GetBytes(value), littleEndian);
    }
    public static byte[] GetBytes(uint value, bool littleEndian)
    {
        return ReverseAsNeeded(BitConverter.GetBytes(value), littleEndian);
    }
    public static byte[] GetBytes(ulong value, bool littleEndian)
    {
        return ReverseAsNeeded(BitConverter.GetBytes(value), littleEndian);
    }
    public static byte[] GetBytes(ushort value, bool littleEndian)
    {
        return ReverseAsNeeded(BitConverter.GetBytes(value), littleEndian);
    }

    private static byte[] ReverseAsNeeded(byte[] bytes, bool wantsLittleEndian)
    {
        if (wantsLittleEndian == BitConverter.IsLittleEndian)
            return bytes;
        else
            return (byte[])bytes.Reverse().ToArray();
    }
}
Community
  • 1
  • 1
Joel Barsotti
  • 3,031
  • 7
  • 35
  • 59
  • I copied this class, it works well and is more intuitive and readable than other solutions. I was wondering if it makes sense or if it is possible to make GetBytes generic ? something like public static byte[] GetBytes(T value, bool littleEndian)... – shelbypereira Sep 17 '20 at 08:37
1

Following is a method to always get big endian byte array quickly.

    public static byte[] GetBigEndianBytes(UInt32 val, bool isLittleEndian)
    {
        UInt32 bigEndian = val;
        if (isLittleEndian)
        {
            bigEndian = (val & 0x000000FFU) << 24 | (val & 0x0000FF00U) << 8 |
                 (val & 0x00FF0000U) >> 8 | (val & 0xFF000000U) >> 24;
        }
        return BitConverter.GetBytes(bigEndian);
    }

This overload can be handy, although care must be taken when working across machines with different endian-nesses.

    public static byte[] GetBigEndianBytes(UInt32 val)
    {
        return GetBigEndianBytes(val, BitConverter.IsLittleEndian);
    }
bytefire
  • 4,156
  • 2
  • 27
  • 36