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

后端 未结 8 804
爱一瞬间的悲伤
爱一瞬间的悲伤 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:56

    Just reverse it, Note that this this code (like the other) works only on a little Endian machine. (edit - that was wrong, since this code returns LE by definition)

      byte[] INT2LE(int data)
      {
         byte[] b = new byte[4];
         b[0] = (byte)data;
         b[1] = (byte)(((uint)data >> 8) & 0xFF);
         b[2] = (byte)(((uint)data >> 16) & 0xFF);
         b[3] = (byte)(((uint)data >> 24) & 0xFF);
         return b;
      }
    

提交回复
热议问题