FastBitConverter.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #if DEBUG && !UNITY_WP_8_1 && !UNITY_WSA_8_1
  2. using System.Runtime.InteropServices;
  3. namespace FlyingWormConsole3.LiteNetLib.Utils
  4. {
  5. public static class FastBitConverter
  6. {
  7. [StructLayout(LayoutKind.Explicit)]
  8. private struct ConverterHelperDouble
  9. {
  10. [FieldOffset(0)]
  11. public ulong Along;
  12. [FieldOffset(0)]
  13. public double Adouble;
  14. }
  15. [StructLayout(LayoutKind.Explicit)]
  16. private struct ConverterHelperFloat
  17. {
  18. [FieldOffset(0)]
  19. public int Aint;
  20. [FieldOffset(0)]
  21. public float Afloat;
  22. }
  23. private static void WriteLittleEndian(byte[] buffer, int offset, ulong data)
  24. {
  25. #if BIGENDIAN
  26. buffer[offset + 7] = (byte)(data);
  27. buffer[offset + 6] = (byte)(data >> 8);
  28. buffer[offset + 5] = (byte)(data >> 16);
  29. buffer[offset + 4] = (byte)(data >> 24);
  30. buffer[offset + 3] = (byte)(data >> 32);
  31. buffer[offset + 2] = (byte)(data >> 40);
  32. buffer[offset + 1] = (byte)(data >> 48);
  33. buffer[offset ] = (byte)(data >> 56);
  34. #else
  35. buffer[offset] = (byte)(data);
  36. buffer[offset + 1] = (byte)(data >> 8);
  37. buffer[offset + 2] = (byte)(data >> 16);
  38. buffer[offset + 3] = (byte)(data >> 24);
  39. buffer[offset + 4] = (byte)(data >> 32);
  40. buffer[offset + 5] = (byte)(data >> 40);
  41. buffer[offset + 6] = (byte)(data >> 48);
  42. buffer[offset + 7] = (byte)(data >> 56);
  43. #endif
  44. }
  45. private static void WriteLittleEndian(byte[] buffer, int offset, int data)
  46. {
  47. #if BIGENDIAN
  48. buffer[offset + 3] = (byte)(data);
  49. buffer[offset + 2] = (byte)(data >> 8);
  50. buffer[offset + 1] = (byte)(data >> 16);
  51. buffer[offset ] = (byte)(data >> 24);
  52. #else
  53. buffer[offset] = (byte)(data);
  54. buffer[offset + 1] = (byte)(data >> 8);
  55. buffer[offset + 2] = (byte)(data >> 16);
  56. buffer[offset + 3] = (byte)(data >> 24);
  57. #endif
  58. }
  59. public static void WriteLittleEndian(byte[] buffer, int offset, short data)
  60. {
  61. #if BIGENDIAN
  62. buffer[offset + 1] = (byte)(data);
  63. buffer[offset ] = (byte)(data >> 8);
  64. #else
  65. buffer[offset] = (byte)(data);
  66. buffer[offset + 1] = (byte)(data >> 8);
  67. #endif
  68. }
  69. public static void GetBytes(byte[] bytes, int startIndex, double value)
  70. {
  71. ConverterHelperDouble ch = new ConverterHelperDouble { Adouble = value };
  72. WriteLittleEndian(bytes, startIndex, ch.Along);
  73. }
  74. public static void GetBytes(byte[] bytes, int startIndex, float value)
  75. {
  76. ConverterHelperFloat ch = new ConverterHelperFloat { Afloat = value };
  77. WriteLittleEndian(bytes, startIndex, ch.Aint);
  78. }
  79. public static void GetBytes(byte[] bytes, int startIndex, short value)
  80. {
  81. WriteLittleEndian(bytes, startIndex, value);
  82. }
  83. public static void GetBytes(byte[] bytes, int startIndex, ushort value)
  84. {
  85. WriteLittleEndian(bytes, startIndex, (short)value);
  86. }
  87. public static void GetBytes(byte[] bytes, int startIndex, int value)
  88. {
  89. WriteLittleEndian(bytes, startIndex, value);
  90. }
  91. public static void GetBytes(byte[] bytes, int startIndex, uint value)
  92. {
  93. WriteLittleEndian(bytes, startIndex, (int)value);
  94. }
  95. public static void GetBytes(byte[] bytes, int startIndex, long value)
  96. {
  97. WriteLittleEndian(bytes, startIndex, (ulong)value);
  98. }
  99. public static void GetBytes(byte[] bytes, int startIndex, ulong value)
  100. {
  101. WriteLittleEndian(bytes, startIndex, value);
  102. }
  103. }
  104. }
  105. #endif