ByteExtension.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*********************************************************************************
  2. *Author: OnClick
  3. *Version: 0.0.1
  4. *UnityVersion: 2017.2.3p3
  5. *Date: 2019-03-14
  6. *Description: IFramework
  7. *History: 2018.11--
  8. *********************************************************************************/
  9. using System;
  10. namespace IFramework.Packets
  11. {
  12. internal static class ByteExtension
  13. {
  14. public static UInt16 ToUInt16(this byte[] array, int offset = 0)
  15. {
  16. return (UInt16)((array[offset] << 8) | array[offset + 1]);
  17. }
  18. public static UInt32 ToUInt32(this byte[] array, int offset = 0)
  19. {
  20. return (((UInt32)array[offset] << 24)
  21. | ((UInt32)array[offset + 1] << 16)
  22. | ((UInt32)array[offset + 2] << 8)
  23. | array[offset + 3]);
  24. }
  25. public static UInt64 ToUInt64(this byte[] array, int offset = 0)
  26. {
  27. return (((UInt64)array[offset] << 56)
  28. | ((UInt64)array[offset + 1] << 48)
  29. | ((UInt64)array[offset + 2] << 40)
  30. | ((UInt64)array[offset + 3] << 32)
  31. | ((UInt64)array[offset + 4] << 24)
  32. | ((UInt64)array[offset + 5] << 16)
  33. | ((UInt64)array[offset + 6] << 8)
  34. | array[offset + 7]);
  35. }
  36. public static byte[] ToBytes(this UInt16 value)
  37. {
  38. return new byte[]{
  39. (byte)(value >> 8),
  40. (byte)value
  41. };
  42. }
  43. public static byte[] ToBytes(this UInt32 value)
  44. {
  45. return new byte[] {
  46. (byte)(value >> 24),
  47. (byte)(value >> 16),
  48. (byte)(value >> 8),
  49. (byte)value
  50. };
  51. }
  52. public static byte[] ToBytes(this UInt64 value)
  53. {
  54. return new byte[]{
  55. (byte)(value >> 56),
  56. (byte)(value >> 48),
  57. (byte)(value >> 40),
  58. (byte)(value >> 32),
  59. (byte)(value >> 24),
  60. (byte)(value >> 16),
  61. (byte)(value >> 8),
  62. (byte)value
  63. };
  64. }
  65. }
  66. }