|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- 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
- {
- /// <summary>
- /// 获取布尔数组指定值得索引
- /// </summary>
- /// <param name="obj">要获取索引的数组</param>
- /// <param name="value">要获取索引的值</param>
- /// <returns></returns>
- public static int GetIndex(this bool[] obj, bool value)
- {
- if (obj == null) return -1;
- return Array.FindIndex(obj, p => p == value);
- }
-
- /// <summary>
- /// 获取字符串数组指定值得索引
- /// </summary>
- /// <param name="obj">要获取索引的数组</param>
- /// <param name="value">要获取索引的值</param>
- /// <returns></returns>
- 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);
- }
-
- /// <summary>
- /// 委托回调
- /// </summary>
- /// <param name="action">要执行的委托</param>
- /// <param name="callback">委托回调</param>
- public static void Invoke(this Action action, Action callback)
- {
- action?.Invoke();
- callback?.Invoke();
- }
-
- /// <summary>
- /// 委托回调
- /// </summary>
- /// <param name="action">要执行的委托</param>
- /// <param name="par">要执行的委托的参数</param>
- /// <param name="callback">委托回调</param>
- public static void Invoke(this Action<object> action, object par, Action callback)
- {
- action?.Invoke(par);
- callback?.Invoke();
- }
-
- public static void Invokes(this Action<object[]> action, object[] par, Action callback)
- {
- action?.Invoke(par);
- callback?.Invoke();
- }
-
-
- /// <summary>
- /// 字节数组转换成32位整数
- /// </summary>
- /// <param name="bytes"></param>
- /// <returns></returns>
- 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;
- }
-
- /// <summary>
- /// 字节数组转换成 ushort 数组
- /// </summary>
- /// <param name="bytes">要转换的字节数组</param>
- /// <param name="reverse">字节高度顺序控制</param>
- /// <returns></returns>
- 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;
- }
-
- /// <summary>
- /// ushort 数组转换成字节数组
- /// </summary>
- /// <param name="src">需要转换的 ushort数组</param>
- /// <param name="reverse">高低字节的设置</param>
- /// <returns></returns>
- 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;
- }
-
-
- /// <summary>
- /// 获取16位整数中指定位的值
- /// </summary>
- /// <param name="data">要获取的整数</param>
- /// <param name="offset">偏移量 范围(1-16)</param>
- /// <returns></returns>
- public static bool Get16bitValue(this ushort data, byte offset)
- {
- if (offset > 16 || offset < 1) return false;
- return (data & 1 << offset - 1) == 0 ? false : true;
- }
-
-
-
-
-
- }
- }
|