当前位置:网站首页>C# CRC校验帮助类
C# CRC校验帮助类
2022-07-19 08:16:00 【ReflexMechanism】
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Rfid_reader
{
public class CRC
{
#region CRC16
public static byte[] CRC16(byte[] data)
{
int len = data.Length;
if (len > 0)
{
ushort crc = 0xFFFF;
for (int i = 0; i < len; i++)
{
crc = (ushort)(crc ^ (data[i]));
for (int j = 0; j < 8; j++)
{
crc = (crc & 1) != 0 ? (ushort)((crc >> 1) ^ 0xA001) : (ushort)(crc >> 1);
}
}
byte hi = (byte)((crc & 0xFF00) >> 8); //高位置
byte lo = (byte)(crc & 0x00FF); //低位置
return new byte[] { hi, lo };
}
return new byte[] { 0, 0 };
}
#endregion
#region ToCRC16
public static string ToCRC16(string content)
{
return ToCRC16(content, Encoding.UTF8);
}
public static string ToCRC16(string content, bool isReverse)
{
return ToCRC16(content, Encoding.UTF8, isReverse);
}
public static string ToCRC16(string content, Encoding encoding)
{
return ByteToString(CRC16(encoding.GetBytes(content)), true);
}
public static string ToCRC16(string content, Encoding encoding, bool isReverse)
{
return ByteToString(CRC16(encoding.GetBytes(content)), isReverse);
}
public static string ToCRC16(byte[] data)
{
return ByteToString(CRC16(data), true);
}
public static string ToCRC16(byte[] data, bool isReverse)
{
return ByteToString(CRC16(data), isReverse);
}
#endregion
#region ToModbusCRC16
public static string ToModbusCRC16(string s)
{
return ToModbusCRC16(s, true);
}
public static string ToModbusCRC16(string s, bool isReverse)
{
return ByteToString(CRC16(StringToHexByte(s)), isReverse);
}
public static string ToModbusCRC16(byte[] data)
{
return ToModbusCRC16(data, true);
}
public static string ToModbusCRC16(byte[] data, bool isReverse)
{
return ByteToString(CRC16(data), isReverse);
}
#endregion
#region ByteToString
public static string ByteToString(byte[] arr, bool isReverse)
{
try
{
byte hi = arr[0], lo = arr[1];
return Convert.ToString(isReverse ? hi + lo * 0x100 : hi * 0x100 + lo, 16).ToUpper().PadLeft(4, '0');
}
catch (Exception ex) { throw (ex); }
}
public static string ByteToString(byte[] arr)
{
try
{
return ByteToString(arr, true);
}
catch (Exception ex) { throw (ex); }
}
#endregion
#region StringToHexString
public static string StringToHexString(string str)
{
StringBuilder s = new StringBuilder();
foreach (short c in str.ToCharArray())
{
s.Append(c.ToString("X4"));
}
return s.ToString();
}
#endregion
#region StringToHexByte
private static string ConvertChinese(string str)
{
StringBuilder s = new StringBuilder();
foreach (short c in str.ToCharArray())
{
if (c <= 0 || c >= 127)
{
s.Append(c.ToString("X4"));
}
else
{
s.Append((char)c);
}
}
return s.ToString();
}
private static string FilterChinese(string str)
{
StringBuilder s = new StringBuilder();
foreach (short c in str.ToCharArray())
{
if (c > 0 && c < 127)
{
s.Append((char)c);
}
}
return s.ToString();
}
/// <summary>
/// 字符串转16进制字符数组
/// </summary>
/// <param name="hex"></param>
/// <returns></returns>
public static byte[] StringToHexByte(string str)
{
return StringToHexByte(str, false);
}
/// <summary>
/// 字符串转16进制字符数组
/// </summary>
/// <param name="str"></param>
/// <param name="isFilterChinese">是否过滤掉中文字符</param>
/// <returns></returns>
public static byte[] StringToHexByte(string str, bool isFilterChinese)
{
string hex = isFilterChinese ? FilterChinese(str) : ConvertChinese(str);
//清除所有空格
hex = hex.Replace(" ", "");
//若字符个数为奇数,补一个0
hex += hex.Length % 2 != 0 ? "0" : "";
byte[] result = new byte[hex.Length / 2];
for (int i = 0, c = result.Length; i < c; i++)
{
result[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return result;
}
#endregion
}
}
边栏推荐
- HCIA-R&S自用笔记(12)路由基础、直连路由与静态路由
- Gauss mathematics -- watching animation and Learning Mathematical Olympiad
- 2022牛客多校联赛第一场 题解
- Synoxos TCP data segment sending process
- DOM series change element content
- 这里看到的SQL会被截断,有没有办法看到完整的?
- GameFi 行业下滑但未出局| June Report
- Description of neural network model - simple neural network model
- 创建文件,文件权限,所有权和粘滞位
- VS2017 OpenCV3.4.2 通过Release的版本 源码编译成 x86
猜你喜欢
DOM introduction and acquisition of DOM series
2022牛客多校联赛第一场 题解
Read the paper: (yolov1) you only look once:unified, real time object detection
MySQL learning notes - stored procedures and functions
Synoxos TCP data segment sending process
动环监控系统价格,动环监控系统价格多少
2022年最新云开发去水印小程序源码
Attribute operation of DOM series elements
MySQL learning notes - View
Spec206 detailed parameters and summary of common problems in the test process (with example operation)
随机推荐
新蓝海“硬防晒”,千亿市场“遮不住”了
链表基础知识对应的三道leetcode题(二)
22. Network principle - key protocol in tcp/ip four layer model (1)
1305_FreeRTOS的队列基本功能描述
Why learn istio
Gamefi industry declines but not out | June report
Distributed notes (04) - redis of distributed locks (distributed lock implementation, locking process, unlocking process, deadlock prevention, correct and incorrect locking and unlocking Implementatio
Source code compilation and installation lamp
Attribute operation of DOM series elements
Essential certificate for product manager!
2022 latest cloud development to watermark applet source code
优化案例2:select标量子查询且主查询排序
Beauty live broadcast first use ldr6028 wireless microphone sound quality transmission OTG charging continuous output
PyQt5学习资源准备与环境配置
数字沙盘的三大应用功能科普
Optimization case 2: select scalar subquery and sort main query
请问如果需要动态提交sql作业并控制启停等该如何实现?
PH测定仪、电导率测定仪、叶绿素荧光测定仪使用
Three layer switching technology
Understand the MySQL architecture design in one article, and don't worry about the interviewer asking too much