Helper.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace TestSeverDemo
{
//包含一些辅助方法,字符串,16进制,byte等转化的方法
public static class Helper
{
//16进制字符串转化为16进制byte数组
public static byte[] StrToHexByte(string hexString)
{
if (string.IsNullOrEmpty(hexString))
{
return null;
}
hexString = hexString.ToUpper();
int length = hexString.Length / 2;
char[] hexChars = hexString.ToCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++)
{
int pos = i * 2;
d[i] = (byte)(charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
private static byte charToByte(char c)
{
return (byte)"0123456789ABCDEF".IndexOf(c);
}
// byte[] to HexString
public static string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
/// <summary>
/// 将16进制转换为有符号的10进制
/// </summary>
/// <param name="hexstr"></param>
/// <returns></returns>
public static string ConvertHexToSIntStr(string hexstr)
{
if (hexstr.StartsWith("0x"))
{
hexstr = hexstr.Substring(2);
}
//如果不是有效的16进制字符串或者字符串长度大于16或者是空,均返回NULL
if (!IsHexadecimal(hexstr) || hexstr.Length > 16 || string.IsNullOrEmpty(hexstr))
{
return null;
}
if (hexstr.Length > 8)
{
return Convert.ToInt64(hexstr, 16).ToString();
}
else if (hexstr.Length > 4)
{
return Convert.ToInt32(hexstr, 16).ToString();
}
else
{
return Convert.ToInt16(hexstr, 16).ToString();
}
}
public static long ConvertHexToSInt(string hexstr)
{
if (hexstr.StartsWith("0x"))
{
hexstr = hexstr.Substring(2);
}
//如果不是有效的16进制字符串或者字符串长度大于16或者是空,均返回NULL
if (!IsHexadecimal(hexstr) || hexstr.Length > 16 || string.IsNullOrEmpty(hexstr))
{
return 99999;
}
if (hexstr.Length > 8)
{
return Convert.ToInt64(hexstr, 16);
}
else if (hexstr.Length > 4)
{
return Convert.ToInt32(hexstr, 16);
}
else
{
return Convert.ToInt16(hexstr, 16);
}
}
//one byte
public static string ConverIntToHexStr2(int t)
{
//显示为2个16进制字符
return t.ToString("X2");
}
//two byte
public static string ConverIntToHexStr4(int t)
{
//显示为4个16进制字符
if (t >= 0)
return t.ToString("X4");
else
{
//负数显示处理(这个是根据实际运行中显示的数据长度进行的处理)
string j = t.ToString("X4");
return j.Substring(j.Length / 2, j.Length / 2);
}
}
/// <summary>
/// 判断是否是十六进制格式字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static bool IsHexadecimal(string str)
{
const string PATTERN = @"[A-Fa-f0-9]+$";
return System.Text.RegularExpressions.Regex.IsMatch(str, PATTERN);
}
public static uint crc16_modbus(byte[] modbusdata, uint length)
{
uint i, j;
uint crc16 = 0xffff;
for (i = 0; i < length; i++)
{
crc16 ^= modbusdata[i];
for (j = 0; j < 8; j++)
{
if ((crc16 & 0x01) == 1)
{
crc16 = (crc16 >> 1) ^ 0xA001;
}
else
{
crc16 = crc16 >> 1;
}
}
}
//little在前,high在后
return ((crc16 & 0xff) << 8) + (crc16 >> 8);
}
//计算crcm
public static uint CRC16(string s)
{
byte[] a = Helper.StrToHexByte(s);
uint len = (uint)a.Length;
uint crc = Helper.crc16_modbus(a, len);
return crc + 4660;
}
}
}