终端一体化运控平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExpandMethod.cs 5.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace BPASmartClient.Helper
  9. {
  10. public static class ExpandMethod
  11. {
  12. /// <summary>
  13. /// 获取布尔数组指定值得索引
  14. /// </summary>
  15. /// <param name="obj">要获取索引的数组</param>
  16. /// <param name="value">要获取索引的值</param>
  17. /// <returns></returns>
  18. public static int GetIndex(this bool[] obj, bool value)
  19. {
  20. if (obj == null) return -1;
  21. return Array.FindIndex(obj, p => p == value);
  22. }
  23. /// <summary>
  24. /// 获取字符串数组指定值得索引
  25. /// </summary>
  26. /// <param name="obj">要获取索引的数组</param>
  27. /// <param name="value">要获取索引的值</param>
  28. /// <returns></returns>
  29. public static int GetIndex(this string[] obj, string value)
  30. {
  31. if (obj == null || value == null) return -1;
  32. return Array.FindIndex(obj, p => p == value && p.Length > 0);
  33. }
  34. /// <summary>
  35. /// 委托回调
  36. /// </summary>
  37. /// <param name="action">要执行的委托</param>
  38. /// <param name="callback">委托回调</param>
  39. public static void Invoke(this Action action, Action callback)
  40. {
  41. action?.Invoke();
  42. callback?.Invoke();
  43. }
  44. /// <summary>
  45. /// 委托回调
  46. /// </summary>
  47. /// <param name="action">要执行的委托</param>
  48. /// <param name="par">要执行的委托的参数</param>
  49. /// <param name="callback">委托回调</param>
  50. public static void Invoke(this Action<object> action, object par, Action callback)
  51. {
  52. action?.Invoke(par);
  53. callback?.Invoke();
  54. }
  55. public static void Invokes(this Action<object[]> action, object[] par, Action callback)
  56. {
  57. action?.Invoke(par);
  58. callback?.Invoke();
  59. }
  60. /// <summary>
  61. /// 字节数组转换成32位整数
  62. /// </summary>
  63. /// <param name="bytes"></param>
  64. /// <returns></returns>
  65. public static int BytesToInt(this byte[] bytes)
  66. {
  67. if (bytes.Length > 4) return -1;
  68. int ReturnVlaue = 0;
  69. for (int i = 0; i < bytes.Length; i++)
  70. {
  71. ReturnVlaue += (int)(bytes[i] << (i * 8));
  72. }
  73. return ReturnVlaue;
  74. }
  75. /// <summary>
  76. /// 字节数组转换成 ushort 数组
  77. /// </summary>
  78. /// <param name="bytes">要转换的字节数组</param>
  79. /// <param name="reverse">字节高度顺序控制</param>
  80. /// <returns></returns>
  81. public static ushort[] BytesToUshorts(this byte[] bytes, bool reverse = false)
  82. {
  83. int len = bytes.Length;
  84. byte[] srcPlus = new byte[len + 1];
  85. bytes.CopyTo(srcPlus, 0);
  86. int count = len >> 1;
  87. if (len % 2 != 0)
  88. {
  89. count += 1;
  90. }
  91. ushort[] dest = new ushort[count];
  92. if (reverse)
  93. {
  94. for (int i = 0; i < count; i++)
  95. {
  96. dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff);
  97. }
  98. }
  99. else
  100. {
  101. for (int i = 0; i < count; i++)
  102. {
  103. dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8);
  104. }
  105. }
  106. return dest;
  107. }
  108. /// <summary>
  109. /// ushort 数组转换成字节数组
  110. /// </summary>
  111. /// <param name="src">需要转换的 ushort数组</param>
  112. /// <param name="reverse">高低字节的设置</param>
  113. /// <returns></returns>
  114. public static byte[] UshortsToBytes(this ushort[] src, bool reverse = false)
  115. {
  116. int count = src.Length;
  117. byte[] dest = new byte[count << 1];
  118. if (reverse)
  119. {
  120. for (int i = 0; i < count; i++)
  121. {
  122. dest[i * 2] = (byte)(src[i] >> 8);
  123. dest[i * 2 + 1] = (byte)(src[i] >> 0);
  124. }
  125. }
  126. else
  127. {
  128. for (int i = 0; i < count; i++)
  129. {
  130. dest[i * 2] = (byte)(src[i] >> 0);
  131. dest[i * 2 + 1] = (byte)(src[i] >> 8);
  132. }
  133. }
  134. return dest;
  135. }
  136. /// <summary>
  137. /// 获取16位整数中指定位的值
  138. /// </summary>
  139. /// <param name="data">要获取的整数</param>
  140. /// <param name="offset">偏移量 范围(1-16)</param>
  141. /// <returns></returns>
  142. public static bool Get16bitValue(this ushort data, byte offset)
  143. {
  144. if (offset > 16 || offset < 1) return false;
  145. return (data & 1 << offset - 1) == 0 ? false : true;
  146. }
  147. public static ushort SetBitValue(this ushort data, byte offset, bool val)
  148. {
  149. if (offset > 16 || offset < 1)
  150. {
  151. return data;
  152. }
  153. int num = 1 << offset - 1;
  154. return (ushort)(val ? (data | num) : (data & ~num));
  155. }
  156. public static bool GetBitValue(this ushort data, byte offset)
  157. {
  158. if (offset > 16 || offset < 1)
  159. {
  160. return false;
  161. }
  162. if ((data & (1 << offset - 1)) != 0)
  163. {
  164. return true;
  165. }
  166. return false;
  167. }
  168. }
  169. }