终端一体化运控平台
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.
 
 
 

313 lines
10 KiB

  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. public static string ToBinString(this object value)
  169. {
  170. int num = 0;
  171. int num2 = 0;
  172. int num3 = 0;
  173. string text = "";
  174. long num4 = 0L;
  175. try
  176. {
  177. if (value.GetType().Name.IndexOf("[]") < 0)
  178. {
  179. string a = value.GetType().Name;
  180. switch (a)
  181. {
  182. case "Byte":
  183. num3 = 7;
  184. num4 = (byte)value;
  185. break;
  186. case "UInt16":
  187. num3 = 15;
  188. num4 = (ushort)value;
  189. break;
  190. case "Int16":
  191. num3 = 15;
  192. num4 = (short)value;
  193. break;
  194. case "UInt32":
  195. num3 = 31;
  196. num4 = (uint)value;
  197. break;
  198. case "Int32":
  199. num3 = 31;
  200. num4 = (int)value;
  201. break;
  202. case "Int64":
  203. num3 = 63;
  204. num4 = (long)value;
  205. break;
  206. default:
  207. throw new Exception();
  208. }
  209. for (num = num3; num >= 0; num += -1)
  210. {
  211. if ((num + 1) % 4 == 0) text = text + " ";
  212. text = (((num4 & (long)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
  213. }
  214. }
  215. else
  216. {
  217. switch (value.GetType().Name)
  218. {
  219. case "Byte[]":
  220. {
  221. num3 = 7;
  222. byte[] array4 = (byte[])value;
  223. for (num2 = 0; num2 <= array4.Length - 1; num2++)
  224. {
  225. for (num = num3; num >= 0; num += -1)
  226. {
  227. text = (((array4[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
  228. }
  229. }
  230. break;
  231. }
  232. case "Int16[]":
  233. {
  234. num3 = 15;
  235. short[] array2 = (short[])value;
  236. for (num2 = 0; num2 <= array2.Length - 1; num2++)
  237. {
  238. for (num = num3; num >= 0; num += -1)
  239. {
  240. text = (((array2[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
  241. }
  242. }
  243. break;
  244. }
  245. case "Int32[]":
  246. {
  247. num3 = 31;
  248. int[] array3 = (int[])value;
  249. for (num2 = 0; num2 <= array3.Length - 1; num2++)
  250. {
  251. for (num = num3; num >= 0; num += -1)
  252. {
  253. text = (((array3[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
  254. }
  255. }
  256. break;
  257. }
  258. case "Int64[]":
  259. {
  260. num3 = 63;
  261. byte[] array = (byte[])value;
  262. for (num2 = 0; num2 <= array.Length - 1; num2++)
  263. {
  264. for (num = num3; num >= 0; num += -1)
  265. {
  266. text = (((array[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
  267. }
  268. }
  269. break;
  270. }
  271. default:
  272. throw new Exception();
  273. }
  274. }
  275. return text;
  276. }
  277. catch
  278. {
  279. return "";
  280. }
  281. }
  282. }
  283. }