using Newtonsoft.Json; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BPASmartClient.Helper { public static class ExpandMethod { /// /// 获取布尔数组指定值得索引 /// /// 要获取索引的数组 /// 要获取索引的值 /// public static int GetIndex(this bool[] obj, bool value) { if (obj == null) return -1; return Array.FindIndex(obj, p => p == value); } /// /// 获取字符串数组指定值得索引 /// /// 要获取索引的数组 /// 要获取索引的值 /// public static int GetIndex(this string[] obj, string value) { if (obj == null || value == null) return -1; return Array.FindIndex(obj, p => p == value && p.Length > 0); } /// /// 委托回调 /// /// 要执行的委托 /// 委托回调 public static void Invoke(this Action action, Action callback) { action?.Invoke(); callback?.Invoke(); } /// /// 委托回调 /// /// 要执行的委托 /// 要执行的委托的参数 /// 委托回调 public static void Invoke(this Action action, object par, Action callback) { action?.Invoke(par); callback?.Invoke(); } public static void Invokes(this Action action, object[] par, Action callback) { action?.Invoke(par); callback?.Invoke(); } /// /// 字节数组转换成32位整数 /// /// /// public static int BytesToInt(this byte[] bytes) { if (bytes.Length > 4) return -1; int ReturnVlaue = 0; for (int i = 0; i < bytes.Length; i++) { ReturnVlaue += (int)(bytes[i] << (i * 8)); } return ReturnVlaue; } /// /// 字节数组转换成 ushort 数组 /// /// 要转换的字节数组 /// 字节高度顺序控制 /// public static ushort[] BytesToUshorts(this byte[] bytes, bool reverse = false) { int len = bytes.Length; byte[] srcPlus = new byte[len + 1]; bytes.CopyTo(srcPlus, 0); int count = len >> 1; if (len % 2 != 0) { count += 1; } ushort[] dest = new ushort[count]; if (reverse) { for (int i = 0; i < count; i++) { dest[i] = (ushort)(srcPlus[i * 2] << 8 | srcPlus[2 * i + 1] & 0xff); } } else { for (int i = 0; i < count; i++) { dest[i] = (ushort)(srcPlus[i * 2] & 0xff | srcPlus[2 * i + 1] << 8); } } return dest; } /// /// ushort 数组转换成字节数组 /// /// 需要转换的 ushort数组 /// 高低字节的设置 /// public static byte[] UshortsToBytes(this ushort[] src, bool reverse = false) { int count = src.Length; byte[] dest = new byte[count << 1]; if (reverse) { for (int i = 0; i < count; i++) { dest[i * 2] = (byte)(src[i] >> 8); dest[i * 2 + 1] = (byte)(src[i] >> 0); } } else { for (int i = 0; i < count; i++) { dest[i * 2] = (byte)(src[i] >> 0); dest[i * 2 + 1] = (byte)(src[i] >> 8); } } return dest; } } }