How can I store 4 8 bit coordinates into one integer (C#)?

后端 未结 4 1082
面向向阳花
面向向阳花 2021-01-28 11:27

Lets say I have the following four variables: player1X, player1Y, player2X, player2Y. These have, for example, respectively the following values: 5, 10, 20, 12. Each of these va

4条回答
  •  梦谈多话
    2021-01-28 12:16

    You can use BitConverter

    To get one Integer out of 4 bytes:

    int i = BitConverter.ToInt32(new byte[] { player1X, player1Y, player2X, player2Y }, 0);
    

    To get the four bytes out of the integer:

    byte[] fourBytes = BitConverter.GetBytes(i);
    

提交回复
热议问题