如何在c#中编写传统校验和的代码? [英] How do I writing code for traditional checksum in c#?

查看:91
本文介绍了如何在c#中编写传统校验和的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写代码来编写传统的校验和c#

其中校验和在IP数据包中工作,校验和字段的值设置为0.然后,整个标题分为16-位部分并加在一起。结果(总和)被补充并插入校验和字段。我尝试编程但没有得到解决方案



我试过这段代码:

 < span class =code-keyword> int  sum =  0 ; 
boolean m = true ;
while (m)
{
string input = Console。的ReadLine();
if (input == eof
{
m = false ;
}
else
{
int 字;
Int32 .TryParse(输入, out 字);
sum = sum + word;
}
}
int left = sum>> 8 ;
int right = sum& 0xFFFF的;
sum = right + left
int checksum = ~sum;

解决方案

将标题转换为 Uint16 的数组(参见声音代码:如何在C#中将byte []转换为short []或float []数组[ ^ ])然后总结这样的数组的所有项目,并最终补充结果(〜运算符( C#参考) [ ^ ])。

I need to write code to programming traditional checksum by c#
Where checksum work in the IP Packet the value of the checksum field is set to 0. Then, the entire header is divided into 16-bit sections and added together. The result (sum) is complemented and inserted into the checksum field. I tried to programming but did not get to the solution

I tried by this code :

int sum = 0;
boolean m = true;
while( m )
{
   string input = Console.ReadLine();
   if( input == "eof" )
   {
      m= false;
   }
   else
   {
      int word;
      Int32.TryParse(input, out word);
      sum = sum + word;
   }
}
int left = sum >> 8;
int right = sum & 0xFFFF;
sum = right + left
int checksum = ~sum;

解决方案

Convert your header to an array of Uint16 (see Sound Code: How to convert byte[] to short[] or float[] arrays in C#[^]) then sum all the items of such array and eventually complement the result ( ~ Operator (C# Reference)[^]).


这篇关于如何在c#中编写传统校验和的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆