终端一体化运控平台
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

160 righe
4.6 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. }
  137. }