c语言中8位和校验程序_C和C ++中的校验和程序
c语言中8位和校验程序
Here you will get checksum program in C and C++.
在这里,您将获得C和C ++的校验和程序。
A checksum is a error detection method in Data Communication. It is used for errors which may have been introduced during transmission or storage. It is usually applied to an installation file after it is received from the download server.
校验和是数据通信中的错误检测方法。 它用于传输或存储期间可能引入的错误。 从下载服务器收到安装文件后,通常将其应用于安装文件。
The actual procedure which yields the checksum, given a data input is called a checksum function or checksum algorithm.
给定数据输入,产生校验和的实际过程称为校验和函数或校验和算法。
Also Read: Hamming Code in C and C++
另请阅读: C和C ++中的汉明代码
Checksum method can only detect errors but is unable to correct the error.
校验和方法只能检测错误,而无法纠正错误。
In this method a checksum is calculated based on the given binary strings which is sent with the data as redundant bits. This data + checksum is received at receiver end and checksum is calculated again, if checksum is 0 it means no error in data received, else there exists some error in the received data.
在这种方法中,根据给定的二进制字符串计算校验和,该二进制字符串与数据一起作为冗余位发送。 此数据+校验和在接收器端接收,并再次计算校验和,如果校验和为0,则表示接收到的数据没有错误,否则接收到的数据中存在一些错误。
For the purpose of this program we are finding checksum for 2 binary strings.
对于此程序,我们发现2个二进制字符串的校验和。
校验和算法 (Checksum Algorithm)
- Take 2 binary input strings. 取2个二进制输入字符串。
- Do their binary sum to find out the checksum which will be sent to the destination or to the receiver. 求和它们的二进制和以找出将被发送到目的地或接收者的校验和。
In binary sum there are 6 cases:-
用二进制和表示6种情况:
- If both bits are 0 and carry is 0, sum=0 and carry=0 如果两个位均为0且进位为0,则sum = 0且进位= 0
- If both bits are 0 and carry is 1,sum=1 and carry=0 如果两个位均为0且进位为1,sum = 1且进位= 0
- If both bits are 1 and carry is 0,sum=0 and carry=1 如果两个位均为1且进位为0,则sum = 0且进位= 1
- If both bits are 1 and carry is 1,sum=1 and carry=1 如果两个位均为1且进位为1,sum = 1和进位= 1
- If either bit is 1 and carry is 0,sum=1 and carry=0 如果任一位为1且进位为0,sum = 1且进位= 0
- If either bit is 1 and carry is 1,sum=0 and carry=1 如果任一位为1且进位为1,sum = 0且进位= 1
- While doing the addition we have to add the binary strings from rightmost end i.e LSB to MSB. 在进行加法时,我们必须从最右端(即LSB)向MSB添加二进制字符串。
- When binary sum is done 1’s complement of it is taken by reversing 1’s to 0’s and vice versa. 当二进制和完成后,通过将1反转为0,反之亦然,可以得到1的补码。
- The resulting 1’s complement is the Checksum. 所得的1的补码是校验和。
- Stop. 停止。
C语言的校验和程序 (Checksum Program in C)
#include<stdio.h>
#include<string.h>
int main()
{
char a[20],b[20];
char sum[20],complement[20];
int i,length;
printf("Enter first binary string\n");
scanf("%s",&a);
printf("Enter second binary string\n");
scanf("%s",&b);
if(strlen(a)==strlen(b)){
length = strlen(a);
char carry='0';
for(i=length-1;i>=0;i--)
{
if(a[i]=='0' && b[i]=='0' && carry=='0')
{
sum[i]='0';
carry='0';
}
else if(a[i]=='0' && b[i]=='0' && carry=='1')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='0')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='1')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='0' && carry=='0')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='1' && b[i]=='0' && carry=='1')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='0')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='1')
{
sum[i]='1';
carry='1';
}
else
break;
}
printf("\nSum=%c%s",carry,sum);
for(i=0;i<length;i++)
{
if(sum[i]=='0')
complement[i]='1';
else
complement[i]='0';
}
if(carry=='1')
carry='0';
else
carry='1';
printf("\nChecksum=%c%s",carry,complement);
}
else {
printf("\nWrong input strings");
}
}
C ++中的校验和程序 (Checksum Program in C++)
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char a[20],b[20];
char sum[20],complement[20];
int i;
cout<<"Enter first binary string\n";
cin>>a;
cout<<"Enter second binary string\n";
cin>>b;
if(strlen(a)==strlen(b))
{
char carry='0';
int length=strlen(a);
for(i=length-1;i>=0;i--)
{
if(a[i]=='0' && b[i]=='0' && carry=='0')
{
sum[i]='0';
carry='0';
}
else if(a[i]=='0' && b[i]=='0' && carry=='1')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='0')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='0' && b[i]=='1' && carry=='1')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='0' && carry=='0')
{
sum[i]='1';
carry='0';
}
else if(a[i]=='1' && b[i]=='0' && carry=='1')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='0')
{
sum[i]='0';
carry='1';
}
else if(a[i]=='1' && b[i]=='1' && carry=='1')
{
sum[i]='1';
carry='1';
}
else
break;
}
cout<<"\nSum="<<carry<<sum;
for(i=0;i<length;i++)
{
if(sum[i]=='0')
complement[i]='1';
else
complement[i]='0';
}
if(carry=='1')
carry='0';
else
carry='1';
cout<<"\nChecksum="<<carry<<complement;
}
else
cout<<"\nWrong input strings";
return 0;
}
Output
输出量
This article is submitted by Rahul Maheshwari. You can connect with him on facebook.
本文由Rahul Maheshwari提交。 您可以在Facebook上与他建立联系。
Comment below if you have queries or found anything incorrect in above checksum program in C and C++.
如果您有疑问或在上述C和C ++的校验和程序中发现任何不正确的内容,请在下面评论。
翻译自: https://www.thecrazyprogrammer.com/2017/02/checksum-program-in-c.html
c语言中8位和校验程序
更多推荐

所有评论(0)