You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
282 lines
8.7 KiB
282 lines
8.7 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace FangYar.IOT_DH_Plugin
|
|
{
|
|
public class CommonHelper
|
|
{
|
|
|
|
#region 公共方法数组操作
|
|
/// <summary>
|
|
/// CRC校验
|
|
/// </summary>
|
|
/// <param name="data">校验数据</param>
|
|
/// <returns>高低8位</returns>
|
|
public static string CRCCalc(string data)
|
|
{
|
|
string[] datas = data.Split(' ');
|
|
List<byte> bytedata = new List<byte>();
|
|
|
|
foreach (string str in datas)
|
|
{
|
|
bytedata.Add(byte.Parse(str, System.Globalization.NumberStyles.AllowHexSpecifier));
|
|
}
|
|
byte[] crcbuf = bytedata.ToArray();
|
|
//计算并填写CRC校验码
|
|
int crc = 0xffff;
|
|
int len = crcbuf.Length;
|
|
for (int n = 0; n < len; n++)
|
|
{
|
|
byte i;
|
|
crc = crc ^ crcbuf[n];
|
|
for (i = 0; i < 8; i++)
|
|
{
|
|
int TT;
|
|
TT = crc & 1;
|
|
crc = crc >> 1;
|
|
crc = crc & 0x7fff;
|
|
if (TT == 1)
|
|
{
|
|
crc = crc ^ 0xa001;
|
|
}
|
|
crc = crc & 0xffff;
|
|
}
|
|
|
|
}
|
|
string[] redata = new string[2];
|
|
redata[1] = Convert.ToString((byte)((crc >> 8) & 0xff), 16);
|
|
redata[0] = Convert.ToString((byte)((crc & 0xff)), 16);
|
|
return redata[0] + " " + redata[1];
|
|
}
|
|
|
|
public static ushort crc_16(byte[] data)
|
|
{
|
|
uint IX, IY;
|
|
ushort crc = 0xFFFF;//set all 1
|
|
|
|
int len = data.Length;
|
|
if (len <= 0)
|
|
crc = 0;
|
|
else
|
|
{
|
|
len--;
|
|
for (IX = 0; IX <= len; IX++)
|
|
{
|
|
crc = (ushort)(crc ^ (data[IX]));
|
|
for (IY = 0; IY <= 7; IY++)
|
|
{
|
|
if ((crc & 1) != 0)
|
|
crc = (ushort)((crc >> 1) ^ 0xA001);
|
|
else
|
|
crc = (ushort)(crc >> 1); //
|
|
}
|
|
}
|
|
}
|
|
|
|
byte buf1 = (byte)((crc & 0xff00) >> 8);//高位置
|
|
byte buf2 = (byte)(crc & 0x00ff); //低位置
|
|
crc = (ushort)(buf1 << 8);
|
|
crc += buf2;
|
|
return crc;
|
|
}
|
|
|
|
///<summary>
|
|
/// 合并数组
|
|
/// </summary>
|
|
/// <param name="First">第一个数组</param>
|
|
/// <param name="Second">第二个数组</param>
|
|
/// <returns>合并后的数组(第一个数组+第二个数组,长度为两个数组的长度)</returns>
|
|
public static byte[] MergerArray(byte[] First, byte[] Second)
|
|
{
|
|
byte[] result = new byte[First.Length + Second.Length];
|
|
First.CopyTo(result, 0);
|
|
Second.CopyTo(result, First.Length);
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 从数组中截取一部分成新的数组
|
|
/// </summary>
|
|
/// <param name="Source">原数组</param>
|
|
/// <param name="StartIndex">原数组的起始位置</param>
|
|
/// <param name="EndIndex">原数组的截止位置</param>
|
|
/// <returns></returns>
|
|
public static byte[] SplitArray(byte[] Source, int StartIndex, int EndIndex)
|
|
{
|
|
try
|
|
{
|
|
byte[] result = new byte[EndIndex - StartIndex + 1];
|
|
for (int i = 0; i <= EndIndex - StartIndex; i++) result[i] = Source[i + StartIndex];
|
|
return result;
|
|
}
|
|
catch (IndexOutOfRangeException ex)
|
|
{
|
|
throw new Exception(ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 截取byte[]
|
|
/// </summary>
|
|
/// <param name="bytes"></param>
|
|
/// <returns></returns>
|
|
public static byte[] byteToData(byte[] bytes, int length)
|
|
{
|
|
byte[] bdata = new byte[length];
|
|
try
|
|
{
|
|
|
|
if (bytes != null)
|
|
{
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
{
|
|
bdata[i] += bytes[i];
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e) { }
|
|
return bdata;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 截取byte[]
|
|
/// </summary>
|
|
/// <param name="bytes"></param>
|
|
/// <returns></returns>
|
|
public static byte[] byteToData(byte[] bytes, int satrt, int end)
|
|
{
|
|
int length = end - satrt + 1;
|
|
byte[] bdata = new byte[length];
|
|
try
|
|
{
|
|
if (bytes != null)
|
|
{
|
|
for (int i = satrt; i <= end; i++)
|
|
{
|
|
bdata[i] += bytes[i];
|
|
}
|
|
}
|
|
|
|
}
|
|
catch (Exception e) { }
|
|
return bdata;
|
|
}
|
|
|
|
// 16进制字符串转字节数组 格式为 string sendMessage = "00 01 00 00 00 06 FF 05 00 64 00 00";
|
|
public static byte[] HexStrTobyte(string hexString)
|
|
{
|
|
hexString = hexString.Replace(" ", "");
|
|
if ((hexString.Length % 2) != 0)
|
|
hexString += " ";
|
|
byte[] returnBytes = new byte[hexString.Length / 2];
|
|
for (int i = 0; i < returnBytes.Length; i++)
|
|
returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2).Trim(), 16);
|
|
return returnBytes;
|
|
}
|
|
|
|
// 字节数组转16进制字符串
|
|
public static string byteToHexStr(byte[] bytes)
|
|
{
|
|
string returnStr = " ";
|
|
if (bytes != null)
|
|
{
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
{
|
|
returnStr += bytes[i].ToString("X2") + " ";//ToString("X2") 为C#中的字符串格式控制符
|
|
}
|
|
}
|
|
return returnStr.Trim(' ');
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// byte转int
|
|
/// </summary>
|
|
/// <param name="src"></param>
|
|
/// <param name="offset"></param>
|
|
/// <returns></returns>
|
|
public static int bytesToInt(byte[] src, int offset)
|
|
{
|
|
int value;
|
|
value = (int)((src[offset] & 0xFF)
|
|
| ((src[offset + 1] & 0xFF) << 8)
|
|
| ((src[offset + 2] & 0xFF) << 16)
|
|
| ((src[offset + 3] & 0xFF) << 24));
|
|
return value;
|
|
}
|
|
/// <summary>
|
|
/// byte转int
|
|
/// </summary>
|
|
/// <param name="bytes"></param>
|
|
|
|
/// <returns></returns>
|
|
public static int bytesToInt(byte[] bytes)
|
|
{
|
|
try
|
|
{
|
|
string str = "";
|
|
for (int i = bytes.Length - 1; i >= 0; i--)
|
|
{
|
|
str += bytes[i].ToString("X2");
|
|
}
|
|
return Convert.ToInt32(str, 16);
|
|
}
|
|
catch (Exception ex) { return 0; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 校验和计算
|
|
/// </summary>
|
|
/// <param name="bs"></param>
|
|
/// <returns></returns>
|
|
public static int CalcSum(byte[] bs)
|
|
{
|
|
int num = 0;
|
|
//所有字节累加
|
|
for (int i = 0; i < bs.Length; i++)
|
|
{
|
|
num = num + bs[i];
|
|
}
|
|
byte ret = (byte)(num & 0xff);//只要最后一个字节
|
|
return ret;
|
|
}
|
|
/// <summary>
|
|
/// 将数据转换成byte[]数组
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public static byte[] Base64tobytes(string code)
|
|
{
|
|
return Convert.FromBase64String(code);
|
|
}
|
|
/// <summary>
|
|
/// 将数据转换成byte[]数组
|
|
/// </summary>
|
|
/// <param name="code"></param>
|
|
/// <returns></returns>
|
|
public static string UnBase64String(byte[] bytes)
|
|
{
|
|
try {
|
|
return Convert.ToBase64String(bytes);
|
|
}
|
|
catch (Exception ex) { return ""; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 将byte[]数组转换成日期
|
|
/// </summary>
|
|
/// <param name="bytes"></param>
|
|
/// <returns></returns>
|
|
public static DateTime BytestoDateTime(byte[] bytes)
|
|
{
|
|
string timestr = "20" + bytes[5].ToString("00") + "/" + bytes[4].ToString("00") + "/" + bytes[3].ToString("00") + " " + bytes[2].ToString("00") + ":" + bytes[1] + ":" + bytes[0];
|
|
|
|
return Convert.ToDateTime(timestr);
|
|
}
|
|
#endregion
|
|
|
|
}
|
|
}
|
|
|