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
{
///
/// 将二进制字符串转换为 Int32 值
///
///
///
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;
}
///
/// 将二进制字符串转换为字节。 可以返回空值。
///
///
///
public static byte? BinStringToByte(this string txt)
{
if (txt.Length == 8)
{
return (byte)txt.BinStringToInt32();
}
return null;
}
///
/// 将值转换为二进制字符串
///
///
///
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 "";
}
}
///
/// 获取字节指定位的值
///
///
///
///
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));
}
}
}