SocketTool.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. namespace Core.KCPTool
  3. {
  4. public class SocketTool
  5. {
  6. public static byte[] LongToByte(long number)
  7. {
  8. byte[] numberBytes = BitConverter.GetBytes(number);
  9. return numberBytes;
  10. }
  11. public static long ByteToLong(byte[] numberBytes)
  12. {
  13. long converted = BitConverter.ToInt64(numberBytes, 0);
  14. return converted;
  15. }
  16. public static int ByteToInt(byte[] numberBytes)
  17. {
  18. int converted = BitConverter.ToInt32(numberBytes, 0);
  19. return converted;
  20. }
  21. public static byte[] IntToByte(int number)
  22. {
  23. byte[] numberBytes = BitConverter.GetBytes(number);
  24. return numberBytes;
  25. }
  26. public static int ByteToInt(byte[] numberBytes, bool isReverse = false)
  27. {
  28. if (isReverse)
  29. {
  30. int converted = BitConverter.ToInt32(numberBytes, 0);
  31. return converted;
  32. }
  33. else
  34. {
  35. int converted = BitConverter.ToInt32(numberBytes, 0);
  36. return converted;
  37. }
  38. }
  39. }
  40. }