|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- using Newtonsoft.Json.Linq;
- using S7.Net.Types;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPASmartClient.S7Net
- {
- public static class Conversion
- {
- /// <summary>
- /// 将二进制字符串转换为 Int32 值
- /// </summary>
- /// <param name="txt"></param>
- /// <returns></returns>
- public static int BinStringToInt32(this string txt)
- {
- int num = 0;
- for (int i = 0; i < txt.Length; i++)
- {
- num = (num << 1) | ((txt[i] == '1') ? 1 : 0);
- }
- return num;
- }
-
- /// <summary>
- /// 将二进制字符串转换为字节。 可以返回空值。
- /// </summary>
- /// <param name="txt"></param>
- /// <returns></returns>
- public static byte? BinStringToByte(this string txt)
- {
- if (txt.Length == 8)
- {
- return (byte)txt.BinStringToInt32();
- }
-
- return null;
- }
-
- /// <summary>
- /// 将值转换为二进制字符串
- /// </summary>
- /// <param name="value"></param>
- /// <returns></returns>
- public static string ValToBinString(this object value)
- {
- int num = 0;
- int num2 = 0;
- int num3 = 0;
- string text = "";
- long num4 = 0L;
- try
- {
- if (value.GetType().Name.IndexOf("[]") < 0)
- {
- switch (value.GetType().Name)
- {
- case "Byte":
- num3 = 7;
- num4 = (byte)value;
- break;
- case "Int16":
- num3 = 15;
- num4 = (short)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)
- {
- 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 "";
- }
- }
-
- /// <summary>
- /// 获取字节指定位的值
- /// </summary>
- /// <param name="data"></param>
- /// <param name="bitPosition"></param>
- /// <returns></returns>
- public static bool SelectBit(this byte data, int bitPosition)
- {
- int num = 1 << bitPosition;
- return (data & num) != 0;
- }
-
- public static short ConvertToShort(this ushort input)
- {
- return short.Parse(input.ToString("X"), NumberStyles.HexNumber);
- }
-
- public static ushort ConvertToUshort(this short input)
- {
- return ushort.Parse(input.ToString("X"), NumberStyles.HexNumber);
- }
-
-
- public static int ConvertToInt(this uint input)
- {
- return int.Parse(input.ToString("X"), NumberStyles.HexNumber);
- }
-
- public static uint ConvertToUInt(this int input)
- {
- return uint.Parse(input.ToString("X"), NumberStyles.HexNumber);
- }
-
- public static uint ConvertToUInt(this float input)
- {
- return DWord.FromByteArray(Real.ToByteArray(input));
- }
-
- public static float ConvertToFloat(this uint input)
- {
- return Real.FromByteArray(DWord.ToByteArray(input));
- }
-
-
- }
- }
|