C#异或校验值

fjsjf 2008-10-29 12:15:46
C#应该怎么算出以下代码的异或校验值。校验 HEX ASCALL码 长度2字节。

0,219.232.191.073",8888**0*CMNET*13823321552*1860*001E*05*05*00*00

...全文
828 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wheeler 2008-10-30
  • 打赏
  • 举报
回复
如果是的话


using System;

namespace mainClass
{
public class mainProgress
{
public static void Main()
{
byte[] msg={1,0,1,1,0,0,1};//信息码
byte[] gmsg=new byte[msg.Length+4];
crc c = new crc();
gmsg=c.code(msg);
Console.Write("编码后字符串为:");
for (int i = 0; i < gmsg.Length; i++)
{
Console.Write("{0}", gmsg[i].ToString());
}
Console.Write("\n");
byte[] gmsg1={ 1, 0, 1, 1, 0, 1, 1 };//接收到的代码
bool r = c.det(gmsg1);
if (r)
{
Console.WriteLine("传输正确");
}
else
{ Console.WriteLine("传输错误"); }
}
}


public class crc//CRC编码类
{
private byte[] g = { 1,1,0,0,1};//生成码
public byte[] code(byte[] msg)//编码
{
byte[] gmsg=new byte[g.Length+msg.Length-1];
msg.CopyTo(gmsg, 0);//
for (int i = 0; i < msg.Length; i++)//完成异或运算,即模2除法
{
if (gmsg[i] == 1)
{
for (int j = 0; j < g.Length; j++)
{
if (gmsg[i + j] == g[j])
gmsg[i + j] = 0;
else
gmsg[i + j] = 1;
}
}
}
msg.CopyTo(gmsg, 0);
return gmsg;
}
private bool f=true;

//接收端检测
public bool det(byte[] gmsg)
{
for (int i = 0; i < gmsg.Length - g.Length+1; i++)
{
if(gmsg[i]==0)
continue;
for (int j = 0; j < g.Length; j++)
{
if (gmsg[i + j] == g[j])
gmsg[i + j] = 0;
else
gmsg[i + j] = 1;
}
}
for (int i = 0; i < gmsg.Length; i++)
{
if (gmsg[i] == 1)
f = false;
}
return f;
}

}
}
wheeler 2008-10-30
  • 打赏
  • 举报
回复
是不是CRC校验?
billlyh 2008-10-30
  • 打赏
  • 举报
回复
我不会,但想学习一下,

111,113

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧