|
- 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;
- }
-
- public static ushort SetBitValue(this ushort data, byte offset, bool val)
- {
- if (offset > 16 || offset < 1)
- {
- return data;
- }
-
- int num = 1 << offset - 1;
- return (ushort)(val ? (data | num) : (data & ~num));
- }
- public static bool GetBitValue(this ushort data, byte offset)
- {
- if (offset > 16 || offset < 1)
- {
- return false;
- }
-
- if ((data & (1 << offset - 1)) != 0)
- {
- return true;
- }
-
- return false;
- }
- public static string ToBinString(this object value)
- {
- int num = 0;
- int num2 = 0;
- int num3 = 0;
- string text = "";
- long num4 = 0L;
- try
- {
- if (value.GetType().Name.IndexOf("[]") < 0)
- {
- string a = value.GetType().Name;
- switch (a)
- {
- case "Byte":
- num3 = 7;
- num4 = (byte)value;
- break;
- case "UInt16":
- num3 = 15;
- num4 = (ushort)value;
- break;
- case "Int16":
- num3 = 15;
- num4 = (short)value;
- break;
- case "UInt32":
- num3 = 31;
- num4 = (uint)value;
- break;
- case "Int32":
- num3 = 31;
- num4 = (int)value;
- break;
- case "Int64":
- num3 = 63;
- num4 = (long)value;
- break;
- default:
- throw new Exception();
- }
-
- for (num = num3; num >= 0; num += -1)
- {
- if ((num + 1) % 4 == 0) text = text + " ";
- text = (((num4 & (long)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
- }
- }
- else
- {
- switch (value.GetType().Name)
- {
- case "Byte[]":
- {
- num3 = 7;
- byte[] array4 = (byte[])value;
- for (num2 = 0; num2 <= array4.Length - 1; num2++)
- {
- for (num = num3; num >= 0; num += -1)
- {
- text = (((array4[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
- }
- }
-
- break;
- }
- case "Int16[]":
- {
- num3 = 15;
- short[] array2 = (short[])value;
- for (num2 = 0; num2 <= array2.Length - 1; num2++)
- {
- for (num = num3; num >= 0; num += -1)
- {
- text = (((array2[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
- }
- }
-
- break;
- }
- case "Int32[]":
- {
- num3 = 31;
- int[] array3 = (int[])value;
- for (num2 = 0; num2 <= array3.Length - 1; num2++)
- {
- for (num = num3; num >= 0; num += -1)
- {
- text = (((array3[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
- }
- }
-
- break;
- }
- case "Int64[]":
- {
- num3 = 63;
- byte[] array = (byte[])value;
- for (num2 = 0; num2 <= array.Length - 1; num2++)
- {
- for (num = num3; num >= 0; num += -1)
- {
- text = (((array[num2] & (byte)Math.Pow(2.0, num)) <= 0) ? (text + "0") : (text + "1"));
- }
- }
-
- break;
- }
- default:
- throw new Exception();
- }
- }
-
- return text;
- }
- catch
- {
- return "";
- }
- }
-
-
-
- }
- }
|