首先弄long int 大小CRC,弄了发现数据太大。接着弄string crc
string crc
#include<iostream>
#include<string>
using namespace std;
string _4bit(char*ct)
{
switch((*ct))
{
case '0':return "0000\0";
case '1':return "0001\0";
case '2':return "0010\0";
case '3':return "0011\0";
case '4':return "0100\0";
case '5':return "0101\0";
case '6':return "0110\0";
case '7':return "0111\0";
case '8':return "1000\0";
case '9':return "1001\0";
case 'A':;case 'a':return "1010\0";
case 'B':;case 'b':return "1011\0";
case 'C':; case 'c':return "1100\0";
case 'D':; case 'd':return "1101\0";
case 'E': ; case 'e':return "1110\0";
case 'F':;case 'f':;
}
return "1111\0";
}
int main()
{
cout<<"待检测数据为十六进制输入16,为十进制输入10,为二进制输入2,其他退出"<<endl;
int choice=0;
cin>>choice;
string the="\0";
char ct;
ct=getchar();
if(16==choice)
{
cout<<"输入待测十六进制数据"<<endl;
while(EOF!=(ct=getchar())&&ct!='\n')
{
if(isdigit(ct)||isalpha(ct))
the+=_4bit(&ct);
else
{
cout<<"输入不合法";
return 0;
}
}
if(the.length()<=0)
{
cout<<"输入有误";
return 0;
}
}else if(10==choice)
{
cout<<"输入待测十进制数据"<<endl;
string temp;
int t=0;
cin>>temp;
for(int i=temp.length()-1;i>=0&&EOF!=temp[i]&&temp[i]!='\n';i--)
{
if(isdigit(temp[i]))
{
t=t+temp[i]-'0';
ct=(t%16<10?t%16+'0':t%16-10+'a');
the=_4bit(&ct)+the;
t/=16;
}
else
{
cout<<"输入不合法";
return 0;
}
}
if(the.length()<=0)
{
cout<<"输入有误";
return 0;
}
}else
if(2==choice)
{
cout<<"输入待测二进制数据"<<endl;
cin>>the;
}
else return 0;
cout<<the<<endl;
int num=0;
bool sign=false;
string scr;
do{
cout<<"冗余位数"<<endl;
cin>>num;
cout<<"输入CRC对应的二进制码"<<endl;
cin>>scr;
if(scr.length()!=num||scr[0]=='0')
{
cout<<"输入CRC和冗余位数不匹配,输入1重新输入其他结束程序"<<endl;
cin>>choice;
if(1==choice)
sign=true;
else return 0;;
}
else
sign=false;
}while(sign);
//============
int i=the.length();
num=0;
for(;num<scr.length()-1;num++)//补零
the+='0';
// ===============
for(num=0;num<i;num++)//异或
{
if('1'==the[num])
{
for(int base=0;base<scr.length();base++)
the[num+base]=(the[num+base]==scr[base]?'0':'1');
}
}
cout<<"0";
for(;num<the.length();num++)
cout<<the[num];
cout<<endl;
return 0;
}
long int crc
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout<<"待检测数据为十六进制输入16,为十进制输入10,其他退出"<<endl;
int choice=0;
long int the;
cin>>choice;
if(16==choice)
{
cout<<"输入待测十六进制数"<<endl;
cin>>hex>>the;
}else if(10==choice)
{
cout<<"输入待测十进制数"<<endl;
cin>>the;
}
else return 0;
cout<<the<<endl;
int num=0;
bool sign=false;
long int crc=0;
do{
cout<<"冗余位数"<<endl;
cin>>dec>>num;
cout<<"输入CRC对应的二进制码"<<endl;
string scr;
cin>>scr;
if(scr.length()!=num||scr[0]=='0')
{
cout<<"输入CRC和冗余位数不匹配,输入1重新输入其他结束程序"<<endl;
cin>>choice;
if(1==choice)
sign=true;
else return 0;;
}
else{
sign=false;
for(int i=0;i<scr.length();i++)
crc=(crc<<1)+scr[i]-'0';
}
}while(sign);
cout<<crc<<endl;
//============
int i=1;
while(0!=(the>>i))i++;
i=i-2;
// cout<<"i:="<<i<<endl;
the=(the<<(num-1));
crc=(crc<<i);
long int base=(1<<(i+1))-1;
i=i+num;
// cout<<i<<" "<<num<<endl;
int t=(the>>(i-1));
while(i>=num)
{
// cout<<t<<endl;
if(1==t)
the=(the^(crc+(the&base)));
i--;
t=(the>>(i-1))%2;
crc=(crc>>1);
base=(base>>1);
// cout<<the<<" "<<(the^(crc+(the&base)))<<" "<<base<<endl;
}
cout<<hex<<(the&((1<<(num-1))-1))<<endl;
// ===============
return 0;
}