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

后端 未结 8 772
爱一瞬间的悲伤
爱一瞬间的悲伤 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条回答
  •  Happy的楠姐
    2020-12-09 08:52

    Could you use the BitConverter class? It will only work on little-endian hardware I believe, but it should handle most of the heavy lifting for you.

    The following is a contrived example that illustrates the use of the class:

    if (BitConverter.IsLittleEndian)
    {
        int someInteger = 100;
        byte[] bytes = BitConverter.GetBytes(someInteger);
        int convertedFromBytes = BitConverter.ToInt32(bytes, 0);
    }
    

提交回复
热议问题