Utils.cs 748 B

1234567891011121314151617181920212223
  1. namespace Telepathy
  2. {
  3. public static class Utils
  4. {
  5. // IntToBytes version that doesn't allocate a new byte[4] each time.
  6. // -> important for MMO scale networking performance.
  7. public static void IntToBytesBigEndianNonAlloc(int value, byte[] bytes, int offset = 0)
  8. {
  9. bytes[offset + 0] = (byte)(value >> 24);
  10. bytes[offset + 1] = (byte)(value >> 16);
  11. bytes[offset + 2] = (byte)(value >> 8);
  12. bytes[offset + 3] = (byte)value;
  13. }
  14. public static int BytesToIntBigEndian(byte[] bytes)
  15. {
  16. return (bytes[0] << 24) |
  17. (bytes[1] << 16) |
  18. (bytes[2] << 8) |
  19. bytes[3];
  20. }
  21. }
  22. }