ByteArrayUtil.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Text;
  3. namespace MavLink
  4. {
  5. internal static class ByteArrayUtil
  6. {
  7. #if MF_FRAMEWORK_VERSION_V4_1
  8. private static readonly MavBitConverter bitConverter = new MavBitConverter();
  9. #else
  10. private static readonly FrameworkBitConverter bitConverter = new FrameworkBitConverter();
  11. #endif
  12. public static byte[] ToChar(byte[] source, int sourceOffset, int size)
  13. {
  14. var bytes = new byte[size];
  15. for (int i = 0; i < size; i++)
  16. bytes[i] = source[i + sourceOffset];
  17. return bytes;
  18. }
  19. public static byte[] ToUInt8(byte[] source, int sourceOffset, int size)
  20. {
  21. var bytes = new byte[size];
  22. Array.Copy(source, sourceOffset, bytes, 0, size);
  23. return bytes;
  24. }
  25. public static sbyte[] ToInt8(byte[] source, int sourceOffset, int size)
  26. {
  27. var bytes = new sbyte[size];
  28. for (int i = 0; i < size; i++)
  29. bytes[i] = unchecked((sbyte)source[i + sourceOffset]);
  30. return bytes;
  31. }
  32. public static UInt16[] ToUInt16(byte[] source, int sourceOffset, int size)
  33. {
  34. var arr = new UInt16[size];
  35. for (int i = 0; i < size; i++)
  36. arr[i] = bitConverter.ToUInt16(source, sourceOffset + (i * sizeof (UInt16)));
  37. return arr;
  38. }
  39. public static Int16[] ToInt16(byte[] source, int sourceOffset, int size)
  40. {
  41. var arr = new Int16[size];
  42. for (int i = 0; i < size; i++)
  43. arr[i] = bitConverter.ToInt16(source, sourceOffset + (i * sizeof(Int16)));
  44. return arr;
  45. }
  46. public static UInt32[] ToUInt32(byte[] source, int sourceOffset, int size)
  47. {
  48. var arr = new UInt32[size];
  49. for (int i = 0; i < size; i++)
  50. arr[i] = bitConverter.ToUInt32(source, sourceOffset + (i * sizeof(UInt32)));
  51. return arr;
  52. }
  53. public static Int32[] ToInt32(byte[] source, int sourceOffset, int size)
  54. {
  55. var arr = new Int32[size];
  56. for (int i = 0; i < size; i++)
  57. arr[i] = bitConverter.ToInt32(source, sourceOffset + (i * sizeof(Int32)));
  58. return arr;
  59. }
  60. public static UInt64[] ToUInt64(byte[] source, int sourceOffset, int size)
  61. {
  62. var arr = new UInt64[size];
  63. for (int i = 0; i < size; i++)
  64. arr[i] = bitConverter.ToUInt64(source, sourceOffset + (i * sizeof(UInt64)));
  65. return arr;
  66. }
  67. public static Int64[] ToInt64(byte[] source, int sourceOffset, int size)
  68. {
  69. var arr = new Int64[size];
  70. for (int i = 0; i < size; i++)
  71. arr[i] = bitConverter.ToInt64(source, sourceOffset + (i * sizeof(Int64)));
  72. return arr;
  73. }
  74. public static Single[] ToSingle(byte[] source, int sourceOffset, int size)
  75. {
  76. var arr = new Single[size];
  77. for (int i = 0; i < size; i++)
  78. arr[i] = bitConverter.ToSingle(source, sourceOffset + (i * sizeof(Single)));
  79. return arr;
  80. }
  81. public static Double[] ToDouble(byte[] source, int sourceOffset, int size)
  82. {
  83. var arr = new Double[size];
  84. for (int i = 0; i < size; i++)
  85. arr[i] = bitConverter.ToDouble(source, sourceOffset + (i * sizeof(Double)));
  86. return arr;
  87. }
  88. public static void ToByteArray(byte[] src, byte[] dst, int offset, int size)
  89. {
  90. int i;
  91. for (i = 0; i < src.Length; i++)
  92. dst[offset + i] = src[i];
  93. while (i++ < size)
  94. dst[offset + i] = 0;
  95. }
  96. public static void ToByteArray(sbyte[] src, byte[] dst, int offset, int size)
  97. {
  98. int i;
  99. for (i = 0; i < size && i<src.Length; i++)
  100. dst[offset + i] = (byte)src[i];
  101. while (i++ < size)
  102. dst[offset + i] = 0;
  103. }
  104. public static void ToByteArray(UInt16[] src, byte[] dst, int offset, int size)
  105. {
  106. for (int i = 0; i < size && i < src.Length; i++)
  107. bitConverter.GetBytes(src[i], dst, offset + (i*sizeof (UInt16)));
  108. }
  109. public static void ToByteArray(Int16[] src, byte[] dst, int offset, int size)
  110. {
  111. for (int i = 0; i < size && i < src.Length; i++)
  112. bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(Int16)));
  113. }
  114. public static void ToByteArray(Int32[] src, byte[] dst, int offset, int size)
  115. {
  116. for (int i = 0; i < size && i < src.Length; i++)
  117. bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(Int32)));
  118. }
  119. public static void ToByteArray(UInt32[] src, byte[] dst, int offset, int size)
  120. {
  121. for (int i = 0; i < size && i < src.Length; i++)
  122. bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(UInt32)));
  123. }
  124. public static void ToByteArray(Single[] src, byte[] dst, int offset, int size)
  125. {
  126. for (int i = 0; i < size && i < src.Length; i++)
  127. bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(Single)));
  128. }
  129. public static void ToByteArray(Double[] src, byte[] dst, int offset, int size)
  130. {
  131. for (int i = 0; i < size && i < src.Length; i++)
  132. bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(Double)));
  133. }
  134. public static void ToByteArray(UInt64[] src, byte[] dst, int offset, int size)
  135. {
  136. for (int i = 0; i < size && i < src.Length; i++)
  137. bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(UInt64)));
  138. }
  139. public static void ToByteArray(Int64[] src, byte[] dst, int offset, int size)
  140. {
  141. for (int i = 0; i < size && i < src.Length; i++)
  142. bitConverter.GetBytes(src[i], dst, offset + (i * sizeof(Int64)));
  143. }
  144. public static string ToString(sbyte[] sbytes)
  145. {
  146. var bytes = new byte[sbytes.Length];
  147. int i;
  148. for ( i = 0; i < bytes.Length && sbytes[i] != '\0'; i++)
  149. bytes[i] = (byte) sbytes[i];
  150. var bytesUntilNull = new byte[i];
  151. Array.Copy(bytes, bytesUntilNull, i);
  152. var encoding = new UTF8Encoding();
  153. return new string(encoding.GetChars(bytesUntilNull));
  154. }
  155. public static string ToString(byte[] bs)
  156. {
  157. int i;
  158. for (i = 0; i < bs.Length && bs[i] != '\0'; i++);
  159. var bytesUntilNull = new byte[i];
  160. Array.Copy(bs, bytesUntilNull, i);
  161. return new string(new UTF8Encoding().GetChars(bytesUntilNull));
  162. }
  163. }
  164. }