Back to list of problems
Software CRC
128.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #define DIVISOR 34943 int main(void) { unsigned char buf[1500]; int len; int i; unsigned int quot; while(fgets((char *)buf, 1024, stdin)) { quot = 0; if (buf[0]=='#') { break; } len = strlen((char *)buf); buf[len-1]=0; buf[len++]=0; if (len%2) { memmove(buf+1, buf, len); buf[0]=0; len++; } for(i=0; i<len; i+=2) { quot *= (unsigned)65536; quot += (unsigned)256*buf[i] + buf[i+1]; quot %= DIVISOR; } if (quot) { quot = DIVISOR-quot; } printf("%02X %02X\n", quot/256, quot%256); } exit(0); }