FrameworkBitConverter.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using System;
  2. namespace MavLink
  3. {
  4. /// <summary>
  5. /// converter from byte[] => primitive CLR types
  6. /// delegates to the .Net framework bitconverter for speed, and to avoid using unsafe pointer
  7. /// casting for Silverlight.
  8. /// </summary>
  9. internal class FrameworkBitConverter
  10. {
  11. private bool _shouldReverse = false;
  12. public void SetDataIsLittleEndian(bool islittle)
  13. {
  14. _shouldReverse = islittle == !BitConverter.IsLittleEndian;
  15. }
  16. public UInt16 ToUInt16(byte[] value, int startIndex)
  17. {
  18. if (_shouldReverse)
  19. {
  20. var bytes = new[] {value[startIndex + 1], value[startIndex]};
  21. return BitConverter.ToUInt16(bytes,0);
  22. }
  23. return BitConverter.ToUInt16(value, startIndex);
  24. }
  25. public Int16 ToInt16(byte[] value, int startIndex)
  26. {
  27. if (_shouldReverse)
  28. {
  29. var bytes = new[] { value[startIndex + 1], value[startIndex] };
  30. return BitConverter.ToInt16(bytes, 0);
  31. }
  32. return BitConverter.ToInt16(value, startIndex);
  33. }
  34. public sbyte ToInt8(byte[] value, int startIndex)
  35. {
  36. return unchecked((sbyte)value[startIndex]);
  37. }
  38. public Int32 ToInt32(byte[] value, int startIndex)
  39. {
  40. if (_shouldReverse)
  41. {
  42. var bytes = new byte[4];
  43. Array.Copy(value,startIndex,bytes,0,4);
  44. Array.Reverse(bytes);
  45. return BitConverter.ToInt32(bytes, 0);
  46. }
  47. return BitConverter.ToInt32(value, startIndex);
  48. }
  49. public UInt32 ToUInt32(byte[] value, int startIndex)
  50. {
  51. if (_shouldReverse)
  52. {
  53. var bytes = new byte[4];
  54. Array.Copy(value, startIndex, bytes, 0, 4);
  55. Array.Reverse(bytes);
  56. return BitConverter.ToUInt32(bytes, 0);
  57. }
  58. return BitConverter.ToUInt32(value, startIndex);
  59. }
  60. public UInt64 ToUInt64(byte[] value, int startIndex)
  61. {
  62. if (_shouldReverse)
  63. {
  64. var bytes = new byte[8];
  65. Array.Copy(value, startIndex, bytes, 0, bytes.Length);
  66. Array.Reverse(bytes);
  67. return BitConverter.ToUInt64(bytes, 0);
  68. }
  69. return BitConverter.ToUInt64(value, startIndex);
  70. }
  71. public Int64 ToInt64(byte[] value, int startIndex)
  72. {
  73. if (_shouldReverse)
  74. {
  75. var bytes = new byte[8];
  76. Array.Copy(value, startIndex, bytes, 0, bytes.Length);
  77. Array.Reverse(bytes);
  78. return BitConverter.ToInt64(bytes, 0);
  79. }
  80. return BitConverter.ToInt64(value, startIndex);
  81. }
  82. public Single ToSingle(byte[] value, int startIndex)
  83. {
  84. if (_shouldReverse)
  85. {
  86. var bytes = new byte[4];
  87. Array.Copy(value, startIndex, bytes, 0, bytes.Length);
  88. Array.Reverse(bytes);
  89. return BitConverter.ToSingle(bytes, 0);
  90. }
  91. return BitConverter.ToSingle(value, startIndex);
  92. }
  93. public Double ToDouble(byte[] value, int startIndex)
  94. {
  95. if (_shouldReverse)
  96. {
  97. var bytes = new byte[8];
  98. Array.Copy(value, startIndex, bytes, 0, bytes.Length);
  99. Array.Reverse(bytes);
  100. return BitConverter.ToDouble(bytes, 0);
  101. }
  102. return BitConverter.ToDouble(value, startIndex);
  103. }
  104. public void GetBytes(Double value, byte[] dst, int offset)
  105. {
  106. var bytes = BitConverter.GetBytes(value);
  107. if (_shouldReverse) Array.Reverse(bytes);
  108. Array.Copy(bytes, 0, dst, offset, bytes.Length);
  109. }
  110. public void GetBytes(Single value, byte[] dst, int offset)
  111. {
  112. var bytes = BitConverter.GetBytes(value);
  113. if (_shouldReverse) Array.Reverse(bytes);
  114. Array.Copy(bytes, 0, dst, offset, bytes.Length);
  115. }
  116. public void GetBytes(UInt64 value, byte[] dst, int offset)
  117. {
  118. var bytes = BitConverter.GetBytes(value);
  119. if (_shouldReverse) Array.Reverse(bytes);
  120. Array.Copy(bytes, 0, dst, offset, bytes.Length);
  121. }
  122. public void GetBytes(Int64 value, byte[] dst, int offset)
  123. {
  124. var bytes = BitConverter.GetBytes(value);
  125. if (_shouldReverse) Array.Reverse(bytes);
  126. Array.Copy(bytes, 0, dst, offset, bytes.Length);
  127. }
  128. public void GetBytes(UInt32 value, byte[] dst, int offset)
  129. {
  130. var bytes = BitConverter.GetBytes(value);
  131. if (_shouldReverse) Array.Reverse(bytes);
  132. Array.Copy(bytes, 0, dst, offset, bytes.Length);
  133. }
  134. public void GetBytes(Int16 value, byte[] dst, int offset)
  135. {
  136. var bytes = BitConverter.GetBytes(value);
  137. if (_shouldReverse) Array.Reverse(bytes);
  138. Array.Copy(bytes, 0, dst, offset, bytes.Length);
  139. }
  140. public void GetBytes(Int32 value, byte[] dst, int offset)
  141. {
  142. var bytes = BitConverter.GetBytes(value);
  143. if (_shouldReverse) Array.Reverse(bytes);
  144. Array.Copy(bytes, 0, dst, offset, bytes.Length);
  145. }
  146. public void GetBytes(UInt16 value, byte[] dst, int offset)
  147. {
  148. var bytes = BitConverter.GetBytes(value);
  149. if (_shouldReverse) Array.Reverse(bytes);
  150. Array.Copy(bytes, 0, dst, offset, bytes.Length);
  151. }
  152. public byte[] GetBytes(sbyte value)
  153. {
  154. return new byte[1]
  155. {
  156. (byte)value,
  157. };
  158. }
  159. }
  160. }