校验码计算方法,就是 1^2^3 的运算,先看效果
看代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CheckCode
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnClear_Click(object sender, EventArgs e)
{
tb16.Text = "";
tbValue.Text = "";
}
private void btnJisuan_Click(object sender, EventArgs e)
{
byte[] bys = strToToHexByte(tb16.Text.Trim());
int va = bys[0];
for (int i = 1; i < bys.Length;i++ )
{
va = va ^ bys[i];
}
tbValue.Text = va.ToString("X2");
}
private byte[] strToToHexByte(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), 16);
return returnBytes;
}
public string byteToHexStr(byte[] bytes)
{
string returnStr = "";
if (bytes != null)
{
for (int i = 0; i < bytes.Length; i++)
{
returnStr += bytes[i].ToString("X2");
}
}
return returnStr;
}
}
}