# Object-c 纯手写方法CRC8,16,32,跟我用笔计算的一样，不过跟那些代码算法结果32位的不同

513人阅读 评论(0)

CRC8正确方法

again只能为2个字，不然的话得自己更改23n-8的位移数目

int again  = 0x3842;

again = again <<8;

NSLog(@"%x",again);

for (int n = 23; n>0; n--) {

if (again >>n & 1) {

NSLog(@"n= %d",n);

again ^= (0x131<<(n-8));

NSLog(@"%x",again);

}else{

again = again;

}

if (again <0x0100) {

NSLog(@"xiao--%d",n);

NSLog(@"again=%x",again);

return 0;

}

}

CRC16正确方法

again只能为4个字，不然的话得自己更改47n-16的位移数目

unsigned long ploy =0x11021;

unsigned long again  = 0x11223344;

again = again <<16;

NSLog(@"%lx",again);

for (int n = 47; n>0; n--) {

if (again >>n & 1) {

NSLog(@"n= %d",n);

again ^= (ploy<<(n-16));

NSLog(@"%lx",again);

}else{

again = again;

}

if (again <0x010000) {

NSLog(@"xiao--%d",n);

NSLog(@"again=%lx",again);

return 0;

}

}

CRC正确方法二，采用分步位移的方法，避免一次性全部位移而超出64.

unsigned long ploy =0x11021;

unsigned long again  = 0x01020304;

for (int j =0; j<5; j++) {

NSLog(@"j%d",j);

if (j!=0) {

again = again << 4;

}

NSLog(@"%lx",again);

for (int n = 31; n>0; n--) {

if (again >>n & 1) {

NSLog(@"n2= %d",n);

again ^= (ploy<<(n-16));

NSLog(@"%lx",again);

}else{

again = again;

}

if (again <0x010000) {

NSLog(@"xiao--%d",n);

NSLog(@"again=%lx",again);

break;

}

}

}

return 0;

crc32的方法

unsigned long ploy =0x104c11db7;

unsigned long again  = 0x40ff048001000000;

for (int j =0; j<9; j++) {

NSLog(@"j%d",j);

if (j!=0) {

again = again << 4;

}

NSLog(@"%lx",again);

for (int n = 63; n>0; n--) {

if (again >>n & 1) {

NSLog(@"n2= %d",n);

again ^= (ploy<<(n-32));

NSLog(@"%lx",again);

}else{

again = again;

}

if (again <0x0100000000) {

NSLog(@"xiao--%d",n);

NSLog(@"again=%lx",again);

break;

}

}

}

return 0;

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：27245次
• 积分：423
• 等级：
• 排名：千里之外
• 原创：7篇
• 转载：51篇
• 译文：0篇
• 评论：0条
文章分类
阅读排行