How to convert an int to a little endian byte array?

后端 未结 8 811
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 08:00

I have this function in C# to convert a little endian byte array to an integer number:

int LE2INT(byte[] data)
{
  return (data[3] << 24) | (data[2] &l         


        
8条回答
  •  难免孤独
    2020-12-09 08:39

    You can use this if you don't want to use new heap allocations:

    public static void Int32ToFourBytes(Int32 number, out byte b0, out byte b1, out byte b2, out byte b3)
    {
        b3 = (byte)number;
        b2 = (byte)(((uint)number >> 8) & 0xFF);
        b1 = (byte)(((uint)number >> 16) & 0xFF);
        b0 = (byte)(((uint)number >> 24) & 0xFF);
    }
    

提交回复
热议问题