1673 lines
60 KiB
C#
1673 lines
60 KiB
C#
using System;
|
||
using System.Collections;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Model.HSL
|
||
{
|
||
public static class ConvertLib
|
||
{
|
||
#region ByteArrayLib
|
||
[Description("根据起始地址和长度自定义截取字节数组")]
|
||
public static byte[] GetByteArrayFromByteArray(byte[] data, int start, int length)
|
||
{
|
||
if (start < 0)
|
||
{
|
||
throw new ArgumentException("开始索引不能为负数");
|
||
}
|
||
|
||
if (length <= 0)
|
||
{
|
||
throw new ArgumentException("长度必须为正数");
|
||
}
|
||
|
||
if (data.Length < start + length)
|
||
{
|
||
throw new ArgumentException("字节数组长度不够或开始索引太大");
|
||
}
|
||
|
||
byte[] array = new byte[length];
|
||
Array.Copy(data, start, array, 0, length);
|
||
return array;
|
||
}
|
||
|
||
[Description("根据起始地址自定义截取字节数组")]
|
||
public static byte[] GetByteArrayFromByteArray(byte[] data, int start)
|
||
{
|
||
return GetByteArrayFromByteArray(data, start, data.Length - start);
|
||
}
|
||
|
||
[Description("从字节数组中截取2个字节,并按指定字节序返回")]
|
||
public static byte[] Get2BytesFromByteArray(byte[] value, int start, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] byteArrayFromByteArray = GetByteArrayFromByteArray(value, start, 2);
|
||
switch (dataFormat)
|
||
{
|
||
case DataFormat.ABCD:
|
||
case DataFormat.CDAB:
|
||
return byteArrayFromByteArray.Reverse().ToArray();
|
||
case DataFormat.BADC:
|
||
case DataFormat.DCBA:
|
||
return byteArrayFromByteArray;
|
||
default:
|
||
return byteArrayFromByteArray;
|
||
}
|
||
}
|
||
|
||
[Description("从字节数组中截取4个字节,并按指定字节序返回")]
|
||
public static byte[] Get4BytesFromByteArray(byte[] value, int start, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] byteArrayFromByteArray = GetByteArrayFromByteArray(value, start, 4);
|
||
byte[] array = new byte[4];
|
||
switch (dataFormat)
|
||
{
|
||
case DataFormat.ABCD:
|
||
array[0] = byteArrayFromByteArray[3];
|
||
array[1] = byteArrayFromByteArray[2];
|
||
array[2] = byteArrayFromByteArray[1];
|
||
array[3] = byteArrayFromByteArray[0];
|
||
break;
|
||
case DataFormat.CDAB:
|
||
array[0] = byteArrayFromByteArray[1];
|
||
array[1] = byteArrayFromByteArray[0];
|
||
array[2] = byteArrayFromByteArray[3];
|
||
array[3] = byteArrayFromByteArray[2];
|
||
break;
|
||
case DataFormat.BADC:
|
||
array[0] = byteArrayFromByteArray[2];
|
||
array[1] = byteArrayFromByteArray[3];
|
||
array[2] = byteArrayFromByteArray[0];
|
||
array[3] = byteArrayFromByteArray[1];
|
||
break;
|
||
case DataFormat.DCBA:
|
||
array = byteArrayFromByteArray;
|
||
break;
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("从字节数组中截取8个字节,并按指定字节序返回")]
|
||
public static byte[] Get8BytesFromByteArray(byte[] value, int start, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] array = new byte[8];
|
||
byte[] byteArrayFromByteArray = GetByteArrayFromByteArray(value, start, 8);
|
||
if (byteArrayFromByteArray == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
switch (dataFormat)
|
||
{
|
||
case DataFormat.ABCD:
|
||
array[0] = byteArrayFromByteArray[7];
|
||
array[1] = byteArrayFromByteArray[6];
|
||
array[2] = byteArrayFromByteArray[5];
|
||
array[3] = byteArrayFromByteArray[4];
|
||
array[4] = byteArrayFromByteArray[3];
|
||
array[5] = byteArrayFromByteArray[2];
|
||
array[6] = byteArrayFromByteArray[1];
|
||
array[7] = byteArrayFromByteArray[0];
|
||
break;
|
||
case DataFormat.CDAB:
|
||
array[0] = byteArrayFromByteArray[1];
|
||
array[1] = byteArrayFromByteArray[0];
|
||
array[2] = byteArrayFromByteArray[3];
|
||
array[3] = byteArrayFromByteArray[2];
|
||
array[4] = byteArrayFromByteArray[5];
|
||
array[5] = byteArrayFromByteArray[4];
|
||
array[6] = byteArrayFromByteArray[7];
|
||
array[7] = byteArrayFromByteArray[6];
|
||
break;
|
||
case DataFormat.BADC:
|
||
array[0] = byteArrayFromByteArray[6];
|
||
array[1] = byteArrayFromByteArray[7];
|
||
array[2] = byteArrayFromByteArray[4];
|
||
array[3] = byteArrayFromByteArray[5];
|
||
array[4] = byteArrayFromByteArray[2];
|
||
array[5] = byteArrayFromByteArray[3];
|
||
array[6] = byteArrayFromByteArray[0];
|
||
array[7] = byteArrayFromByteArray[1];
|
||
break;
|
||
case DataFormat.DCBA:
|
||
array = byteArrayFromByteArray;
|
||
break;
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("比较两个字节数组是否完全相同")]
|
||
public static bool GetByteArrayEquals(byte[] value1, byte[] value2)
|
||
{
|
||
if (value1 == null || value2 == null)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
if (value1.Length != value2.Length)
|
||
{
|
||
return false;
|
||
}
|
||
|
||
for (int i = 0; i < value1.Length; i++)
|
||
{
|
||
if (value1[i] != value2[i])
|
||
{
|
||
return false;
|
||
}
|
||
}
|
||
|
||
return true;
|
||
}
|
||
|
||
[Description("将单个字节转换成字节数组")]
|
||
public static byte[] GetByteArrayFromByte(byte value)
|
||
{
|
||
return new byte[1] { value };
|
||
}
|
||
|
||
[Description("将Short类型数值转换成字节数组")]
|
||
public static byte[] GetByteArrayFromShort(short value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] bytes = BitConverter.GetBytes(value);
|
||
byte[] array = new byte[2];
|
||
switch (dataFormat)
|
||
{
|
||
case DataFormat.ABCD:
|
||
case DataFormat.CDAB:
|
||
array[0] = bytes[1];
|
||
array[1] = bytes[0];
|
||
break;
|
||
case DataFormat.BADC:
|
||
case DataFormat.DCBA:
|
||
array = bytes;
|
||
break;
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将UShort类型数值转换成字节数组")]
|
||
public static byte[] GetByteArrayFromUShort(ushort value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] bytes = BitConverter.GetBytes(value);
|
||
byte[] array = new byte[2];
|
||
switch (dataFormat)
|
||
{
|
||
case DataFormat.ABCD:
|
||
case DataFormat.CDAB:
|
||
array[0] = bytes[1];
|
||
array[1] = bytes[0];
|
||
break;
|
||
case DataFormat.BADC:
|
||
case DataFormat.DCBA:
|
||
array = bytes;
|
||
break;
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将Int类型数值转换成字节数组")]
|
||
public static byte[] GetByteArrayFromInt(int value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] bytes = BitConverter.GetBytes(value);
|
||
byte[] array = new byte[4];
|
||
switch (dataFormat)
|
||
{
|
||
case DataFormat.ABCD:
|
||
array[0] = bytes[3];
|
||
array[1] = bytes[2];
|
||
array[2] = bytes[1];
|
||
array[3] = bytes[0];
|
||
break;
|
||
case DataFormat.CDAB:
|
||
array[0] = bytes[1];
|
||
array[1] = bytes[0];
|
||
array[2] = bytes[3];
|
||
array[3] = bytes[2];
|
||
break;
|
||
case DataFormat.BADC:
|
||
array[0] = bytes[2];
|
||
array[1] = bytes[3];
|
||
array[2] = bytes[0];
|
||
array[3] = bytes[1];
|
||
break;
|
||
case DataFormat.DCBA:
|
||
array = bytes;
|
||
break;
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将UInt类型数值转换成字节数组")]
|
||
public static byte[] GetByteArrayFromUInt(uint value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] bytes = BitConverter.GetBytes(value);
|
||
byte[] array = new byte[4];
|
||
switch (dataFormat)
|
||
{
|
||
case DataFormat.ABCD:
|
||
array[0] = bytes[3];
|
||
array[1] = bytes[2];
|
||
array[2] = bytes[1];
|
||
array[3] = bytes[0];
|
||
break;
|
||
case DataFormat.CDAB:
|
||
array[0] = bytes[1];
|
||
array[1] = bytes[0];
|
||
array[2] = bytes[3];
|
||
array[3] = bytes[2];
|
||
break;
|
||
case DataFormat.BADC:
|
||
array[0] = bytes[2];
|
||
array[1] = bytes[3];
|
||
array[2] = bytes[0];
|
||
array[3] = bytes[1];
|
||
break;
|
||
case DataFormat.DCBA:
|
||
array = bytes;
|
||
break;
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将Float数值转换成字节数组")]
|
||
public static byte[] GetByteArrayFromFloat(float value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] bytes = BitConverter.GetBytes(value);
|
||
byte[] array = new byte[4];
|
||
switch (dataFormat)
|
||
{
|
||
case DataFormat.ABCD:
|
||
array[0] = bytes[3];
|
||
array[1] = bytes[2];
|
||
array[2] = bytes[1];
|
||
array[3] = bytes[0];
|
||
break;
|
||
case DataFormat.CDAB:
|
||
array[0] = bytes[1];
|
||
array[1] = bytes[0];
|
||
array[2] = bytes[3];
|
||
array[3] = bytes[2];
|
||
break;
|
||
case DataFormat.BADC:
|
||
array[0] = bytes[2];
|
||
array[1] = bytes[3];
|
||
array[2] = bytes[0];
|
||
array[3] = bytes[1];
|
||
break;
|
||
case DataFormat.DCBA:
|
||
array = bytes;
|
||
break;
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将Double类型数值转换成字节数组")]
|
||
public static byte[] GetByteArrayFromDouble(double value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] bytes = BitConverter.GetBytes(value);
|
||
byte[] array = new byte[8];
|
||
switch (dataFormat)
|
||
{
|
||
case DataFormat.ABCD:
|
||
array[0] = bytes[7];
|
||
array[1] = bytes[6];
|
||
array[2] = bytes[5];
|
||
array[3] = bytes[4];
|
||
array[4] = bytes[3];
|
||
array[5] = bytes[2];
|
||
array[6] = bytes[1];
|
||
array[7] = bytes[0];
|
||
break;
|
||
case DataFormat.CDAB:
|
||
array[0] = bytes[1];
|
||
array[1] = bytes[0];
|
||
array[2] = bytes[3];
|
||
array[3] = bytes[2];
|
||
array[4] = bytes[5];
|
||
array[5] = bytes[4];
|
||
array[6] = bytes[7];
|
||
array[7] = bytes[6];
|
||
break;
|
||
case DataFormat.BADC:
|
||
array[0] = bytes[6];
|
||
array[1] = bytes[7];
|
||
array[2] = bytes[4];
|
||
array[3] = bytes[5];
|
||
array[4] = bytes[2];
|
||
array[5] = bytes[3];
|
||
array[6] = bytes[0];
|
||
array[7] = bytes[1];
|
||
break;
|
||
case DataFormat.DCBA:
|
||
array = bytes;
|
||
break;
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将Long类型数值转换成字节数组")]
|
||
public static byte[] GetByteArrayFromLong(long value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] bytes = BitConverter.GetBytes(value);
|
||
byte[] array = new byte[8];
|
||
switch (dataFormat)
|
||
{
|
||
case DataFormat.ABCD:
|
||
array[0] = bytes[7];
|
||
array[1] = bytes[6];
|
||
array[2] = bytes[5];
|
||
array[3] = bytes[4];
|
||
array[4] = bytes[3];
|
||
array[5] = bytes[2];
|
||
array[6] = bytes[1];
|
||
array[7] = bytes[0];
|
||
break;
|
||
case DataFormat.CDAB:
|
||
array[0] = bytes[1];
|
||
array[1] = bytes[0];
|
||
array[2] = bytes[3];
|
||
array[3] = bytes[2];
|
||
array[4] = bytes[5];
|
||
array[5] = bytes[4];
|
||
array[6] = bytes[7];
|
||
array[7] = bytes[6];
|
||
break;
|
||
case DataFormat.BADC:
|
||
array[0] = bytes[6];
|
||
array[1] = bytes[7];
|
||
array[2] = bytes[4];
|
||
array[3] = bytes[5];
|
||
array[4] = bytes[2];
|
||
array[5] = bytes[3];
|
||
array[6] = bytes[0];
|
||
array[7] = bytes[1];
|
||
break;
|
||
case DataFormat.DCBA:
|
||
array = bytes;
|
||
break;
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将ULong类型数值转换成字节数组")]
|
||
public static byte[] GetByteArrayFromULong(ulong value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] bytes = BitConverter.GetBytes(value);
|
||
byte[] array = new byte[8];
|
||
switch (dataFormat)
|
||
{
|
||
case DataFormat.ABCD:
|
||
array[0] = bytes[7];
|
||
array[1] = bytes[6];
|
||
array[2] = bytes[5];
|
||
array[3] = bytes[4];
|
||
array[4] = bytes[3];
|
||
array[5] = bytes[2];
|
||
array[6] = bytes[1];
|
||
array[7] = bytes[0];
|
||
break;
|
||
case DataFormat.CDAB:
|
||
array[0] = bytes[1];
|
||
array[1] = bytes[0];
|
||
array[2] = bytes[3];
|
||
array[3] = bytes[2];
|
||
array[4] = bytes[5];
|
||
array[5] = bytes[4];
|
||
array[6] = bytes[7];
|
||
array[7] = bytes[6];
|
||
break;
|
||
case DataFormat.BADC:
|
||
array[0] = bytes[6];
|
||
array[1] = bytes[7];
|
||
array[2] = bytes[4];
|
||
array[3] = bytes[5];
|
||
array[4] = bytes[2];
|
||
array[5] = bytes[3];
|
||
array[6] = bytes[0];
|
||
array[7] = bytes[1];
|
||
break;
|
||
case DataFormat.DCBA:
|
||
array = bytes;
|
||
break;
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将Short数组转换成字节数组")]
|
||
public static byte[] GetByteArrayFromShortArray(short[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
ByteArray byteArray = new ByteArray();
|
||
foreach (short value2 in value)
|
||
{
|
||
byteArray.Add(GetByteArrayFromShort(value2, dataFormat));
|
||
}
|
||
|
||
return byteArray.array;
|
||
}
|
||
|
||
[Description("将UShort数组转换成字节数组")]
|
||
public static byte[] GetByteArrayFromUShortArray(ushort[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
ByteArray byteArray = new ByteArray();
|
||
foreach (ushort value2 in value)
|
||
{
|
||
byteArray.Add(GetByteArrayFromUShort(value2, dataFormat));
|
||
}
|
||
|
||
return byteArray.array;
|
||
}
|
||
|
||
[Description("将Int类型数组转换成字节数组")]
|
||
public static byte[] GetByteArrayFromIntArray(int[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
ByteArray byteArray = new ByteArray();
|
||
foreach (int value2 in value)
|
||
{
|
||
byteArray.Add(GetByteArrayFromInt(value2, dataFormat));
|
||
}
|
||
|
||
return byteArray.array;
|
||
}
|
||
|
||
[Description("将UInt类型数组转换成字节数组")]
|
||
public static byte[] GetByteArrayFromUIntArray(uint[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
ByteArray byteArray = new ByteArray();
|
||
foreach (uint value2 in value)
|
||
{
|
||
byteArray.Add(GetByteArrayFromUInt(value2, dataFormat));
|
||
}
|
||
|
||
return byteArray.array;
|
||
}
|
||
|
||
[Description("将Float类型数组转成字节数组")]
|
||
public static byte[] GetByteArrayFromFloatArray(float[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
ByteArray byteArray = new ByteArray();
|
||
foreach (float value2 in value)
|
||
{
|
||
byteArray.Add(GetByteArrayFromFloat(value2, dataFormat));
|
||
}
|
||
|
||
return byteArray.array;
|
||
}
|
||
|
||
[Description("将Double类型数组转成字节数组")]
|
||
public static byte[] GetByteArrayFromDoubleArray(double[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
ByteArray byteArray = new ByteArray();
|
||
foreach (double value2 in value)
|
||
{
|
||
byteArray.Add(GetByteArrayFromDouble(value2, dataFormat));
|
||
}
|
||
|
||
return byteArray.array;
|
||
}
|
||
|
||
[Description("将Long类型数组转换成字节数组")]
|
||
public static byte[] GetByteArrayFromLongArray(long[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
ByteArray byteArray = new ByteArray();
|
||
foreach (long num in value)
|
||
{
|
||
byteArray.Add(GetByteArrayFromDouble(num, dataFormat));
|
||
}
|
||
|
||
return byteArray.array;
|
||
}
|
||
|
||
[Description("将ULong类型数组转换成字节数组")]
|
||
public static byte[] GetByteArrayFromULongArray(ulong[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
ByteArray byteArray = new ByteArray();
|
||
foreach (ulong value2 in value)
|
||
{
|
||
byteArray.Add(GetByteArrayFromULong(value2, dataFormat));
|
||
}
|
||
|
||
return byteArray.array;
|
||
}
|
||
|
||
[Description("将指定编码格式的字符串转换成字节数组")]
|
||
public static byte[] GetByteArrayFromString(string value, Encoding encoding)
|
||
{
|
||
return encoding.GetBytes(value);
|
||
}
|
||
|
||
[Description("将16进制字符串按照空格分隔成字节数组")]
|
||
public static byte[] GetByteArrayFromHexString(string value, string spilt = " ")
|
||
{
|
||
value = value.Trim();
|
||
List<byte> list = new List<byte>();
|
||
try
|
||
{
|
||
if (value.Contains(spilt))
|
||
{
|
||
string[] array = value.Split(new string[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
|
||
string[] array2 = array;
|
||
foreach (string text in array2)
|
||
{
|
||
list.Add(Convert.ToByte(text.Trim(), 16));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
list.Add(Convert.ToByte(value.Trim(), 16));
|
||
}
|
||
|
||
return list.ToArray();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentNullException("数据转换失败:" + ex.Message);
|
||
}
|
||
}
|
||
|
||
[Description("将16进制字符串不用分隔符转换成字节数组(每2个字符为1个字节)")]
|
||
public static byte[] GetByteArrayFromHexStringWithoutSpilt(string value)
|
||
{
|
||
if (value.Length % 2 != 0)
|
||
{
|
||
throw new ArgumentNullException("检查字符串长度是否为偶数");
|
||
}
|
||
|
||
List<byte> list = new List<byte>();
|
||
try
|
||
{
|
||
for (int i = 0; i < value.Length; i += 2)
|
||
{
|
||
string value2 = value.Substring(i, 2);
|
||
list.Add(Convert.ToByte(value2, 16));
|
||
}
|
||
|
||
return list.ToArray();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentNullException("数据转换失败:" + ex.Message);
|
||
}
|
||
}
|
||
|
||
[Description("将byte数据转换成一个Asii格式字节数组")]
|
||
public static byte[] GetAsciiByteArrayFromValue(byte value)
|
||
{
|
||
return Encoding.ASCII.GetBytes(value.ToString("X2"));
|
||
}
|
||
|
||
[Description("将short数据转换成一个Ascii格式字节数组")]
|
||
public static byte[] GetAsciiByteArrayFromValue(short value)
|
||
{
|
||
return Encoding.ASCII.GetBytes(value.ToString("X4"));
|
||
}
|
||
|
||
[Description("将ushort数据转换成一个Ascii格式字节数组")]
|
||
public static byte[] GetAsciiByteArrayFromValue(ushort value)
|
||
{
|
||
return Encoding.ASCII.GetBytes(value.ToString("X4"));
|
||
}
|
||
|
||
[Description("将string数据转换成一个Ascii格式字节数组")]
|
||
public static byte[] GetAsciiByteArrayFromValue(string value)
|
||
{
|
||
return Encoding.ASCII.GetBytes(value);
|
||
}
|
||
|
||
[Description("将布尔数组转换成字节数组")]
|
||
public static byte[] GetByteArrayFromBoolArray(bool[] data)
|
||
{
|
||
if (data == null || data.Length == 0)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否正确");
|
||
}
|
||
|
||
byte[] array = new byte[(data.Length % 8 != 0) ? (data.Length / 8 + 1) : (data.Length / 8)];
|
||
for (int i = 0; i < array.Length; i++)
|
||
{
|
||
int num = ((data.Length < 8 * (i + 1)) ? (data.Length - 8 * i) : 8);
|
||
for (int j = 0; j < num; j++)
|
||
{
|
||
array[i] = SetbitValue(array[i], j, data[8 * i + j]);
|
||
}
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将西门子字符串转换成字节数组")]
|
||
public static byte[] GetByteArrayFromSiemensString(string value)
|
||
{
|
||
byte[] byteArrayFromString = GetByteArrayFromString(value, Encoding.GetEncoding("GBK"));
|
||
byte[] array = new byte[byteArrayFromString.Length + 2];
|
||
array[0] = (byte)(byteArrayFromString.Length + 2);
|
||
array[1] = (byte)byteArrayFromString.Length;
|
||
Array.Copy(byteArrayFromString, 0, array, 2, byteArrayFromString.Length);
|
||
return array;
|
||
}
|
||
|
||
[Description("将欧姆龙CIP字符串转换成字节数组")]
|
||
public static byte[] GetByteArrayFromOmronCIPString(string data)
|
||
{
|
||
byte[] byteArrayFromString = GetByteArrayFromString(data, Encoding.ASCII);
|
||
byte[] evenByteArray = GetEvenByteArray(byteArrayFromString);
|
||
byte[] array = new byte[evenByteArray.Length + 2];
|
||
array[0] = BitConverter.GetBytes(array.Length - 2)[0];
|
||
array[1] = BitConverter.GetBytes(array.Length - 2)[1];
|
||
Array.Copy(evenByteArray, 0, array, 2, evenByteArray.Length);
|
||
return array;
|
||
}
|
||
|
||
[Description("扩展为偶数长度字节数组")]
|
||
public static byte[] GetEvenByteArray(byte[] data)
|
||
{
|
||
if (data == null)
|
||
{
|
||
return new byte[0];
|
||
}
|
||
|
||
if (data.Length % 2 != 0)
|
||
{
|
||
return GetFixedLengthByteArray(data, data.Length + 1);
|
||
}
|
||
|
||
return data;
|
||
}
|
||
|
||
[Description("扩展或压缩字节数组到指定数量")]
|
||
public static byte[] GetFixedLengthByteArray(byte[] data, int length)
|
||
{
|
||
if (data == null)
|
||
{
|
||
return new byte[length];
|
||
}
|
||
|
||
if (data.Length == length)
|
||
{
|
||
return data;
|
||
}
|
||
|
||
byte[] array = new byte[length];
|
||
Array.Copy(data, array, Math.Min(data.Length, array.Length));
|
||
return array;
|
||
}
|
||
|
||
[Description("将字节数组转换成Ascii字节数组")]
|
||
public static byte[] GetAsciiBytesFromByteArray(byte[] value, string segment = "")
|
||
{
|
||
return Encoding.ASCII.GetBytes(GetHexStringFromByteArray(value, segment));
|
||
}
|
||
|
||
[Description("将Ascii字节数组转换成字节数组")]
|
||
public static byte[] GetBytesArrayFromAsciiByteArray(byte[] value)
|
||
{
|
||
return GetByteArrayFromHexStringWithoutSpilt(Encoding.ASCII.GetString(value));
|
||
}
|
||
|
||
[Description("将2个字节数组进行合并")]
|
||
public static byte[] GetByteArrayFromTwoByteArray(byte[] bytes1, byte[] bytes2)
|
||
{
|
||
if (bytes1 == null && bytes2 == null)
|
||
{
|
||
return null;
|
||
}
|
||
|
||
if (bytes1 == null)
|
||
{
|
||
return bytes2;
|
||
}
|
||
|
||
if (bytes2 == null)
|
||
{
|
||
return bytes1;
|
||
}
|
||
|
||
byte[] array = new byte[bytes1.Length + bytes2.Length];
|
||
bytes1.CopyTo(array, 0);
|
||
bytes2.CopyTo(array, bytes1.Length);
|
||
return array;
|
||
}
|
||
|
||
[Description("将3个字节数组进行合并")]
|
||
public static byte[] GetByteArrayFromThreeByteArray(byte[] bytes1, byte[] bytes2, byte[] bytes3)
|
||
{
|
||
return GetByteArrayFromTwoByteArray(GetByteArrayFromTwoByteArray(bytes1, bytes2), bytes3);
|
||
}
|
||
|
||
[Description("将字节数组中的某个数据修改")]
|
||
public static byte[] SetByteArray(byte[] sourceArray, object value, int start, int offset)
|
||
{
|
||
string name = value.GetType().Name;
|
||
byte[] array = null;
|
||
switch (name.ToLower())
|
||
{
|
||
case "boolean":
|
||
Array.Copy(GetByteArrayFromByte(SetbitValue(sourceArray[start], offset, Convert.ToBoolean(value))), 0, sourceArray, start, 1);
|
||
break;
|
||
case "byte":
|
||
Array.Copy(GetByteArrayFromByte(Convert.ToByte(value)), 0, sourceArray, start, 1);
|
||
break;
|
||
case "int16":
|
||
Array.Copy(GetByteArrayFromShort(Convert.ToInt16(value)), 0, sourceArray, start, 2);
|
||
break;
|
||
case "uint16":
|
||
Array.Copy(GetByteArrayFromUShort(Convert.ToUInt16(value)), 0, sourceArray, start, 2);
|
||
break;
|
||
case "int32":
|
||
Array.Copy(GetByteArrayFromInt(Convert.ToInt32(value)), 0, sourceArray, start, 4);
|
||
break;
|
||
case "uint32":
|
||
Array.Copy(GetByteArrayFromUInt(Convert.ToUInt32(value)), 0, sourceArray, start, 4);
|
||
break;
|
||
case "single":
|
||
Array.Copy(GetByteArrayFromFloat(Convert.ToSingle(value)), 0, sourceArray, start, 4);
|
||
break;
|
||
case "double":
|
||
Array.Copy(GetByteArrayFromDouble(Convert.ToDouble(value)), 0, sourceArray, start, 8);
|
||
break;
|
||
case "int64":
|
||
Array.Copy(GetByteArrayFromLong(Convert.ToInt64(value)), 0, sourceArray, start, 8);
|
||
break;
|
||
case "uint64":
|
||
Array.Copy(GetByteArrayFromULong(Convert.ToUInt64(value)), 0, sourceArray, start, 8);
|
||
break;
|
||
case "byte[]":
|
||
array = GetByteArrayFromHexString(value.ToString());
|
||
Array.Copy(array, 0, sourceArray, start, array.Length);
|
||
break;
|
||
case "int16[]":
|
||
array = GetByteArrayFromShortArray(GetShortArrayFromString(value.ToString()));
|
||
Array.Copy(array, 0, sourceArray, start, array.Length);
|
||
break;
|
||
case "uint16[]":
|
||
array = GetByteArrayFromUShortArray(GetUShortArrayFromString(value.ToString()));
|
||
Array.Copy(array, 0, sourceArray, start, array.Length);
|
||
break;
|
||
case "int32[]":
|
||
array = GetByteArrayFromIntArray(GetIntArrayFromString(value.ToString()));
|
||
Array.Copy(array, 0, sourceArray, start, array.Length);
|
||
break;
|
||
case "uint32[]":
|
||
array = GetByteArrayFromUIntArray(GetUIntArrayFromString(value.ToString()));
|
||
Array.Copy(array, 0, sourceArray, start, array.Length);
|
||
break;
|
||
case "single[]":
|
||
array = GetByteArrayFromFloatArray(GetFloatArrayFromString(value.ToString()));
|
||
Array.Copy(array, 0, sourceArray, start, array.Length);
|
||
break;
|
||
case "double[]":
|
||
array = GetByteArrayFromDoubleArray(GetDoubleArrayFromString(value.ToString()));
|
||
Array.Copy(array, 0, sourceArray, start, array.Length);
|
||
break;
|
||
case "int64[]":
|
||
array = GetByteArrayFromLongArray(GetLongArrayFromString(value.ToString()));
|
||
Array.Copy(array, 0, sourceArray, start, array.Length);
|
||
break;
|
||
case "uint64[]":
|
||
array = GetByteArrayFromULongArray(GetULongArrayFromString(value.ToString()));
|
||
Array.Copy(array, 0, sourceArray, start, array.Length);
|
||
break;
|
||
}
|
||
|
||
return sourceArray;
|
||
}
|
||
#endregion
|
||
|
||
#region FloatLib
|
||
[Description("将字节数组中某4个字节转换成Float类型")]
|
||
public static float GetFloatFromByteArray(byte[] value, int start = 0, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] value2 = Get4BytesFromByteArray(value, start, dataFormat);
|
||
return BitConverter.ToSingle(value2, 0);
|
||
}
|
||
|
||
[Description("将字节数组转换成Float数组")]
|
||
public static float[] GetFloatArrayFromByteArray(byte[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
if (value == null)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为空");
|
||
}
|
||
|
||
if (value.Length % 4 != 0)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为4的倍数");
|
||
}
|
||
|
||
float[] array = new float[value.Length / 4];
|
||
for (int i = 0; i < value.Length / 4; i++)
|
||
{
|
||
array[i] = GetFloatFromByteArray(value, 4 * i, dataFormat);
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将Float字符串转换成单精度浮点型数组")]
|
||
public static float[] GetFloatArrayFromString(string value, string spilt = " ")
|
||
{
|
||
value = value.Trim();
|
||
List<float> list = new List<float>();
|
||
try
|
||
{
|
||
if (value.Contains(spilt))
|
||
{
|
||
string[] array = value.Split(new string[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
|
||
string[] array2 = array;
|
||
foreach (string text in array2)
|
||
{
|
||
list.Add(Convert.ToSingle(text.Trim()));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
list.Add(Convert.ToSingle(value.Trim()));
|
||
}
|
||
|
||
return list.ToArray();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentNullException("数据转换失败:" + ex.Message);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region BitLib
|
||
[Description("返回某个字节的指定位")]
|
||
public static bool GetBitFromByte(byte value, int offset)
|
||
{
|
||
return (value & (1 << offset)) != 0;
|
||
}
|
||
|
||
[Description("获取字节数组(长度为2)中的指定位")]
|
||
public static bool GetBitFrom2Bytes(byte[] value, int offset, bool isLittleEndian = true)
|
||
{
|
||
if (value.Length < 2)
|
||
{
|
||
throw new ArgumentException("数组长度小于2");
|
||
}
|
||
|
||
if (isLittleEndian)
|
||
{
|
||
return GetBitFrom2Bytes(value[1], value[0], offset);
|
||
}
|
||
|
||
return GetBitFrom2Bytes(value[0], value[1], offset);
|
||
}
|
||
|
||
[Description("获取高低字节的指定位")]
|
||
public static bool GetBitFrom2Bytes(byte high, byte low, int offset)
|
||
{
|
||
if (offset >= 0 && offset <= 7)
|
||
{
|
||
return GetBitFromByte(low, offset);
|
||
}
|
||
|
||
return GetBitFromByte(high, offset - 8);
|
||
}
|
||
|
||
[Description("返回字节数组中某个字节的指定位")]
|
||
public static bool GetBitFromByteArray(byte[] value, int start, int offset)
|
||
{
|
||
if (start > value.Length - 1)
|
||
{
|
||
throw new ArgumentException("数组长度不够或开始索引太大");
|
||
}
|
||
|
||
return GetBitFromByte(value[start], offset);
|
||
}
|
||
|
||
[Description("返回字节数组中某2个字节的指定位")]
|
||
public static bool GetBitFrom2BytesArray(byte[] value, int start, int offset, bool isLittleEndian = true)
|
||
{
|
||
if (start > value.Length - 2)
|
||
{
|
||
throw new ArgumentException("数组长度不够或开始索引太大");
|
||
}
|
||
|
||
byte[] value2 = new byte[2]
|
||
{
|
||
value[start],
|
||
value[start + 1]
|
||
};
|
||
return GetBitFrom2Bytes(value2, offset, isLittleEndian);
|
||
}
|
||
|
||
[Description("根据一个Short返回指定位")]
|
||
public static bool GetBitFromShort(short value, int offset, bool isLittleEndian = true)
|
||
{
|
||
return GetBitFrom2Bytes(BitConverter.GetBytes(value), offset, !isLittleEndian);
|
||
}
|
||
|
||
[Description("根据一个UShort返回指定位")]
|
||
public static bool GetBitFromUShort(ushort value, int offset, bool isLittleEndian = true)
|
||
{
|
||
return GetBitFrom2Bytes(BitConverter.GetBytes(value), offset, !isLittleEndian);
|
||
}
|
||
|
||
[Description("将字节数组转换成布尔数组")]
|
||
public static bool[] GetBitArrayFromByteArray(byte[] value, int length)
|
||
{
|
||
return GetBitArrayFromByteArray(value, 0, length);
|
||
}
|
||
|
||
[Description("将字节数组转换成布尔数组")]
|
||
public static bool[] GetBitArrayFromByteArray(byte[] value, int start, int length)
|
||
{
|
||
if (length <= 0)
|
||
{
|
||
throw new ArgumentException("长度必须为正数");
|
||
}
|
||
|
||
if (start < 0)
|
||
{
|
||
throw new ArgumentException("开始索引必须为非负数");
|
||
}
|
||
|
||
if (start + length > value.Length * 8)
|
||
{
|
||
throw new ArgumentException("数组长度不够或长度太大");
|
||
}
|
||
|
||
BitArray bitArray = new BitArray(value);
|
||
bool[] array = new bool[length];
|
||
for (int i = 0; i < length; i++)
|
||
{
|
||
array[i] = bitArray[i + start];
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将字节数组转换成布尔数组")]
|
||
public static bool[] GetBitArrayFromByteArray(byte[] value)
|
||
{
|
||
return GetBitArrayFromByteArray(value, value.Length * 8);
|
||
}
|
||
|
||
[Description("将一个字节转换成布尔数组")]
|
||
public static bool[] GetBitArrayFromByte(byte value)
|
||
{
|
||
return GetBitArrayFromByteArray(new byte[1] { value });
|
||
}
|
||
|
||
[Description("根据位开始和长度截取布尔数组")]
|
||
public static bool[] GetBitArrayFromBitArray(bool[] value, int start, int length)
|
||
{
|
||
if (start < 0)
|
||
{
|
||
throw new ArgumentException("开始索引不能为负数");
|
||
}
|
||
|
||
if (length <= 0)
|
||
{
|
||
throw new ArgumentException("长度必须为正数");
|
||
}
|
||
|
||
if (value.Length < start + length)
|
||
{
|
||
throw new ArgumentException("数组长度不够或开始索引太大");
|
||
}
|
||
|
||
bool[] array = new bool[length];
|
||
Array.Copy(value, start, array, 0, length);
|
||
return array;
|
||
}
|
||
|
||
[Description("将字符串按照指定的分隔符转换成布尔数组")]
|
||
public static bool[] GetBitArrayFromBitArrayString(string value, string spilt = " ")
|
||
{
|
||
value = value.Trim();
|
||
List<bool> list = new List<bool>();
|
||
if (value.Contains(spilt))
|
||
{
|
||
string[] array = value.Split(new string[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
|
||
string[] array2 = array;
|
||
foreach (string value2 in array2)
|
||
{
|
||
list.Add(IsBoolean(value2));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
list.Add(IsBoolean(value));
|
||
}
|
||
|
||
return list.ToArray();
|
||
}
|
||
|
||
[Description("判断是否为布尔")]
|
||
public static bool IsBoolean(string value)
|
||
{
|
||
return value == "1" || value.ToLower() == "true";
|
||
}
|
||
#endregion
|
||
|
||
#region IntLib
|
||
[Description("字节数组中截取转成32位整型")]
|
||
public static int GetIntFromByteArray(byte[] value, int start = 0, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] value2 = Get4BytesFromByteArray(value, start, dataFormat);
|
||
return BitConverter.ToInt32(value2, 0);
|
||
}
|
||
|
||
[Description("将字节数组中截取转成32位整型数组")]
|
||
public static int[] GetIntArrayFromByteArray(byte[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
if (value == null)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为空");
|
||
}
|
||
|
||
if (value.Length % 4 != 0)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为4的倍数");
|
||
}
|
||
|
||
int[] array = new int[value.Length / 4];
|
||
for (int i = 0; i < value.Length / 4; i++)
|
||
{
|
||
array[i] = GetIntFromByteArray(value, 4 * i, dataFormat);
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将字符串转转成32位整型数组")]
|
||
public static int[] GetIntArrayFromString(string value, string spilt = " ")
|
||
{
|
||
value = value.Trim();
|
||
List<int> list = new List<int>();
|
||
try
|
||
{
|
||
if (value.Contains(spilt))
|
||
{
|
||
string[] array = value.Split(new string[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
|
||
string[] array2 = array;
|
||
foreach (string text in array2)
|
||
{
|
||
list.Add(Convert.ToInt32(text.Trim()));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
list.Add(Convert.ToInt32(value.Trim()));
|
||
}
|
||
|
||
return list.ToArray();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentNullException("数据转换失败:" + ex.Message);
|
||
}
|
||
}
|
||
|
||
[Description("通过布尔长度取整数")]
|
||
public static int GetByteLengthFromBoolLength(int boolLength)
|
||
{
|
||
return (boolLength % 8 == 0) ? (boolLength / 8) : (boolLength / 8 + 1);
|
||
}
|
||
#endregion
|
||
|
||
#region ShortLib
|
||
[Description("字节数组中截取转成16位整型")]
|
||
public static short GetShortFromByteArray(byte[] value, int start = 0, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] value2 = Get2BytesFromByteArray(value, start, dataFormat);
|
||
return BitConverter.ToInt16(value2, 0);
|
||
}
|
||
|
||
[Description("将字节数组中截取转成16位整型数组")]
|
||
public static short[] GetShortArrayFromByteArray(byte[] value, DataFormat type = DataFormat.ABCD)
|
||
{
|
||
if (value == null)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为空");
|
||
}
|
||
|
||
if (value.Length % 2 != 0)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为偶数");
|
||
}
|
||
|
||
short[] array = new short[value.Length / 2];
|
||
for (int i = 0; i < array.Length; i++)
|
||
{
|
||
array[i] = GetShortFromByteArray(value, i * 2, type);
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将字符串转转成16位整型数组")]
|
||
public static short[] GetShortArrayFromString(string value, string spilt = " ")
|
||
{
|
||
value = value.Trim();
|
||
List<short> list = new List<short>();
|
||
try
|
||
{
|
||
if (value.Contains(spilt))
|
||
{
|
||
string[] array = value.Split(new string[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
|
||
string[] array2 = array;
|
||
foreach (string text in array2)
|
||
{
|
||
list.Add(Convert.ToInt16(text.Trim()));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
list.Add(Convert.ToInt16(value.Trim()));
|
||
}
|
||
|
||
return list.ToArray();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentNullException("数据转换失败:" + ex.Message);
|
||
}
|
||
}
|
||
|
||
[Description("设置字节数组某个位")]
|
||
public static short SetBitValueFrom2ByteArray(byte[] value, int offset, bool bitVal, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
if (offset >= 0 && offset <= 7)
|
||
{
|
||
value[1] = SetbitValue(value[1], offset, bitVal);
|
||
}
|
||
else
|
||
{
|
||
value[0] = SetbitValue(value[0], offset - 8, bitVal);
|
||
}
|
||
|
||
return GetShortFromByteArray(value, 0, dataFormat);
|
||
}
|
||
|
||
[Description("设置16位整型某个位")]
|
||
public static short SetBitValueFromShort(short value, int offset, bool bitVal, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] byteArrayFromShort = GetByteArrayFromShort(value, dataFormat);
|
||
return SetBitValueFrom2ByteArray(byteArrayFromShort, offset, bitVal, dataFormat);
|
||
}
|
||
#endregion
|
||
|
||
#region UShortLib
|
||
[Description("字节数组中截取转成16位无符号整型")]
|
||
public static ushort GetUShortFromByteArray(byte[] value, int start = 0, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] value2 = Get2BytesFromByteArray(value, start, dataFormat);
|
||
return BitConverter.ToUInt16(value2, 0);
|
||
}
|
||
|
||
[Description("将字节数组中截取转成16位无符号整型数组")]
|
||
public static ushort[] GetUShortArrayFromByteArray(byte[] value, DataFormat type = DataFormat.ABCD)
|
||
{
|
||
if (value == null)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为空");
|
||
}
|
||
|
||
if (value.Length % 2 != 0)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为偶数");
|
||
}
|
||
|
||
ushort[] array = new ushort[value.Length / 2];
|
||
for (int i = 0; i < array.Length; i++)
|
||
{
|
||
array[i] = GetUShortFromByteArray(value, i * 2, type);
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将字符串转转成16位无符号整型数组")]
|
||
public static ushort[] GetUShortArrayFromString(string value, string spilt = " ")
|
||
{
|
||
value = value.Trim();
|
||
List<ushort> list = new List<ushort>();
|
||
try
|
||
{
|
||
if (value.Contains(spilt))
|
||
{
|
||
string[] array = value.Split(new string[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
|
||
string[] array2 = array;
|
||
foreach (string text in array2)
|
||
{
|
||
list.Add(Convert.ToUInt16(text.Trim()));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
list.Add(Convert.ToUInt16(value.Trim()));
|
||
}
|
||
|
||
return list.ToArray();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentNullException("数据转换失败:" + ex.Message);
|
||
}
|
||
}
|
||
|
||
[Description("设置16位整型某个位")]
|
||
public static ushort SetBitValueFromUShort(ushort value, int offset, bool bitVal, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] byteArrayFromUShort = GetByteArrayFromUShort(value, dataFormat);
|
||
return (ushort)SetBitValueFrom2ByteArray(byteArrayFromUShort, offset, bitVal, dataFormat);
|
||
}
|
||
#endregion
|
||
|
||
#region UIntLib
|
||
[Description("字节数组中截取转成32位无符号整型")]
|
||
public static uint GetUIntFromByteArray(byte[] value, int start = 0, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] value2 = Get4BytesFromByteArray(value, start, dataFormat);
|
||
return BitConverter.ToUInt32(value2, 0);
|
||
}
|
||
|
||
[Description("将字节数组中截取转成32位无符号整型数组")]
|
||
public static uint[] GetUIntArrayFromByteArray(byte[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
if (value == null)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为空");
|
||
}
|
||
|
||
if (value.Length % 4 != 0)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为4的倍数");
|
||
}
|
||
|
||
uint[] array = new uint[value.Length / 4];
|
||
for (int i = 0; i < value.Length / 4; i++)
|
||
{
|
||
array[i] = GetUIntFromByteArray(value, 4 * i, dataFormat);
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将字符串转转成32位无符号整型数组")]
|
||
public static uint[] GetUIntArrayFromString(string value, string spilt = " ")
|
||
{
|
||
value = value.Trim();
|
||
List<uint> list = new List<uint>();
|
||
try
|
||
{
|
||
if (value.Contains(spilt))
|
||
{
|
||
string[] array = value.Split(new string[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
|
||
string[] array2 = array;
|
||
foreach (string text in array2)
|
||
{
|
||
list.Add(Convert.ToUInt32(text.Trim()));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
list.Add(Convert.ToUInt32(value.Trim()));
|
||
}
|
||
|
||
return list.ToArray();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentNullException("数据转换失败:" + ex.Message);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region ByteLib
|
||
[Description("将字节中的某个位赋值")]
|
||
public static byte SetbitValue(byte value, int offset, bool bitValue)
|
||
{
|
||
return bitValue ? ((byte)(value | (byte)Math.Pow(2.0, offset))) : ((byte)(value & ~(byte)Math.Pow(2.0, offset)));
|
||
}
|
||
|
||
[Description("从字节数组中截取某个字节")]
|
||
public static byte GetByteFromByteArray(byte[] value, int start)
|
||
{
|
||
if (start > value.Length - 1)
|
||
{
|
||
throw new ArgumentException("字节数组长度不够或开始索引太大");
|
||
}
|
||
|
||
return value[start];
|
||
}
|
||
|
||
[Description("将布尔数组转换成字节数组")]
|
||
public static byte GetByteFromBoolArray(bool[] value)
|
||
{
|
||
if (value.Length != 8)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为8");
|
||
}
|
||
|
||
byte b = 0;
|
||
for (int i = 0; i < 8; i++)
|
||
{
|
||
b = SetbitValue(b, i, value[i]);
|
||
}
|
||
|
||
return b;
|
||
}
|
||
#endregion
|
||
|
||
#region StringLib
|
||
[Description("将字节数组转换成字符串")]
|
||
public static string GetStringFromByteArrayByBitConvert(byte[] value, int start, int count)
|
||
{
|
||
return BitConverter.ToString(value, start, count);
|
||
}
|
||
|
||
[Description("将字节数组转换成字符串")]
|
||
public static string GetStringFromByteArrayByBitConvert(byte[] value)
|
||
{
|
||
return BitConverter.ToString(value, 0, value.Length);
|
||
}
|
||
|
||
[Description("将字节数组转换成带编码格式字符串")]
|
||
public static string GetStringFromByteArrayByEncoding(byte[] value, int start, int count, Encoding encoding)
|
||
{
|
||
return encoding.GetString(GetByteArrayFromByteArray(value, start, count));
|
||
}
|
||
|
||
[Description("将字节数组转换成带编码格式字符串")]
|
||
public static string GetStringFromByteArrayByEncoding(byte[] value, Encoding encoding)
|
||
{
|
||
return encoding.GetString(GetByteArrayFromByteArray(value, 0, value.Length));
|
||
}
|
||
|
||
[Description("根据起始地址和长度将字节数组转换成带16进制字符串")]
|
||
public static string GetHexStringFromByteArray(byte[] value, int start, int count, string segment = " ")
|
||
{
|
||
byte[] byteArrayFromByteArray = GetByteArrayFromByteArray(value, start, count);
|
||
StringBuilder stringBuilder = new StringBuilder();
|
||
byte[] array = byteArrayFromByteArray;
|
||
foreach (byte b in array)
|
||
{
|
||
if (segment.Length == 0)
|
||
{
|
||
stringBuilder.Append($"{b:X2}");
|
||
}
|
||
else
|
||
{
|
||
stringBuilder.Append($"{b:X2}{segment}");
|
||
}
|
||
}
|
||
|
||
if (segment.Length != 0 && stringBuilder.Length > 1 && stringBuilder.ToString().Substring(stringBuilder.Length - segment.Length) == segment)
|
||
{
|
||
stringBuilder.Remove(stringBuilder.Length - segment.Length, segment.Length);
|
||
}
|
||
|
||
return stringBuilder.ToString();
|
||
}
|
||
|
||
[Description("将整个字节数组转换成带16进制字符串")]
|
||
public static string GetHexStringFromByteArray(byte[] source, string segment = " ")
|
||
{
|
||
return GetHexStringFromByteArray(source, 0, source.Length, segment);
|
||
}
|
||
|
||
[Description("将字节数组转换成西门子字符串")]
|
||
public static string GetSiemensStringFromByteArray(byte[] source, int start, int length, string emptyStr = "empty")
|
||
{
|
||
byte[] byteArrayFromByteArray = GetByteArrayFromByteArray(source, start, length + 2);
|
||
int num = byteArrayFromByteArray[1];
|
||
if (num > 0)
|
||
{
|
||
return Encoding.GetEncoding("GBK").GetString(GetByteArrayFromByteArray(byteArrayFromByteArray, 2, num));
|
||
}
|
||
|
||
return emptyStr;
|
||
}
|
||
|
||
[Description("根据起始地址和长度将各种类型数组转换成字符串")]
|
||
public static string GetStringFromValueArray<T>(T[] value, int start, int length, string segment = " ")
|
||
{
|
||
if (start < 0)
|
||
{
|
||
throw new ArgumentException("开始索引不能为负数");
|
||
}
|
||
|
||
if (length <= 0)
|
||
{
|
||
throw new ArgumentException("长度必须为正数");
|
||
}
|
||
|
||
if (value.Length < start + length)
|
||
{
|
||
throw new ArgumentException("字节数组长度不够或开始索引太大");
|
||
}
|
||
|
||
T[] array = new T[length];
|
||
Array.Copy(value, start, array, 0, length);
|
||
return GetStringFromValueArray(array, segment);
|
||
}
|
||
|
||
[Description("各种类型数组转换成字符串")]
|
||
public static string GetStringFromValueArray<T>(T[] value, string segment = " ")
|
||
{
|
||
StringBuilder stringBuilder = new StringBuilder();
|
||
if (value.Length != 0)
|
||
{
|
||
for (int i = 0; i < value.Length; i++)
|
||
{
|
||
T val = value[i];
|
||
if (segment.Length == 0)
|
||
{
|
||
stringBuilder.Append(val.ToString());
|
||
}
|
||
else
|
||
{
|
||
stringBuilder.Append(val.ToString() + segment.ToString());
|
||
}
|
||
}
|
||
}
|
||
|
||
if (segment.Length != 0 && stringBuilder.Length > 1 && stringBuilder.ToString().Substring(stringBuilder.Length - segment.Length) == segment)
|
||
{
|
||
stringBuilder.Remove(stringBuilder.Length - segment.Length, segment.Length);
|
||
}
|
||
|
||
return stringBuilder.ToString();
|
||
}
|
||
#endregion
|
||
|
||
#region ULongLib
|
||
[Description("字节数组中截取转成64位无符号整型")]
|
||
public static ulong GetULongFromByteArray(byte[] value, int start = 0, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] value2 = Get8BytesFromByteArray(value, start, dataFormat);
|
||
return BitConverter.ToUInt64(value2, 0);
|
||
}
|
||
|
||
[Description("将字节数组中截取转成64位无符号整型数组")]
|
||
public static ulong[] GetULongArrayFromByteArray(byte[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
if (value == null)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为空");
|
||
}
|
||
|
||
if (value.Length % 8 != 0)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为8的倍数");
|
||
}
|
||
|
||
ulong[] array = new ulong[value.Length / 8];
|
||
for (int i = 0; i < value.Length / 8; i++)
|
||
{
|
||
array[i] = GetULongFromByteArray(value, 8 * i, dataFormat);
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将字符串转转成64位无符号整型数组")]
|
||
public static ulong[] GetULongArrayFromString(string value, string spilt = " ")
|
||
{
|
||
value = value.Trim();
|
||
List<ulong> list = new List<ulong>();
|
||
try
|
||
{
|
||
if (value.Contains(spilt))
|
||
{
|
||
string[] array = value.Split(new string[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
|
||
string[] array2 = array;
|
||
foreach (string text in array2)
|
||
{
|
||
list.Add(Convert.ToUInt64(text.Trim()));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
list.Add(Convert.ToUInt64(value.Trim()));
|
||
}
|
||
|
||
return list.ToArray();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentNullException("数据转换失败:" + ex.Message);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region LongLib
|
||
[Description("字节数组中截取转成64位整型")]
|
||
public static long GetLongFromByteArray(byte[] value, int start = 0, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] value2 = Get8BytesFromByteArray(value, start, dataFormat);
|
||
return BitConverter.ToInt64(value2, 0);
|
||
}
|
||
|
||
[Description("将字节数组中截取转成64位整型数组")]
|
||
public static long[] GetLongArrayFromByteArray(byte[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
if (value == null)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为空");
|
||
}
|
||
|
||
if (value.Length % 8 != 0)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为8的倍数");
|
||
}
|
||
|
||
long[] array = new long[value.Length / 8];
|
||
for (int i = 0; i < value.Length / 8; i++)
|
||
{
|
||
array[i] = GetLongFromByteArray(value, 8 * i, dataFormat);
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将字符串转转成64位整型数组")]
|
||
public static long[] GetLongArrayFromString(string value, string spilt = " ")
|
||
{
|
||
value = value.Trim();
|
||
List<long> list = new List<long>();
|
||
try
|
||
{
|
||
if (value.Contains(spilt))
|
||
{
|
||
string[] array = value.Split(new string[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
|
||
string[] array2 = array;
|
||
foreach (string text in array2)
|
||
{
|
||
list.Add(Convert.ToInt64(text.Trim()));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
list.Add(Convert.ToInt64(value.Trim()));
|
||
}
|
||
|
||
return list.ToArray();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentNullException("数据转换失败:" + ex.Message);
|
||
}
|
||
}
|
||
#endregion
|
||
|
||
#region DoubleLib
|
||
[Description("将字节数组中某8个字节转换成Double类型")]
|
||
public static double GetDoubleFromByteArray(byte[] value, int start = 0, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
byte[] value2 = Get8BytesFromByteArray(value, start, dataFormat);
|
||
return BitConverter.ToDouble(value2, 0);
|
||
}
|
||
|
||
[Description("将字节数组转换成Double数组")]
|
||
public static double[] GetDoubleArrayFromByteArray(byte[] value, DataFormat dataFormat = DataFormat.ABCD)
|
||
{
|
||
if (value == null)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为空");
|
||
}
|
||
|
||
if (value.Length % 8 != 0)
|
||
{
|
||
throw new ArgumentNullException("检查数组长度是否为8的倍数");
|
||
}
|
||
|
||
double[] array = new double[value.Length / 8];
|
||
for (int i = 0; i < value.Length / 8; i++)
|
||
{
|
||
array[i] = GetDoubleFromByteArray(value, 8 * i, dataFormat);
|
||
}
|
||
|
||
return array;
|
||
}
|
||
|
||
[Description("将Double字符串转换成双精度浮点型数组")]
|
||
public static double[] GetDoubleArrayFromString(string value, string spilt = " ")
|
||
{
|
||
value = value.Trim();
|
||
List<double> list = new List<double>();
|
||
try
|
||
{
|
||
if (value.Contains(spilt))
|
||
{
|
||
string[] array = value.Split(new string[1] { spilt }, StringSplitOptions.RemoveEmptyEntries);
|
||
string[] array2 = array;
|
||
foreach (string text in array2)
|
||
{
|
||
list.Add(Convert.ToDouble(text.Trim()));
|
||
}
|
||
}
|
||
else
|
||
{
|
||
list.Add(Convert.ToDouble(value.Trim()));
|
||
}
|
||
|
||
return list.ToArray();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new ArgumentNullException("数据转换失败:" + ex.Message);
|
||
}
|
||
}
|
||
#endregion
|
||
}
|
||
}
|