int parity (char msg[1400]) {
int parity = 0;
int i,j;
char c;
for(i=0;i<strlen(msg);i++) {
for(j=0;j<8;j++) {
c = msg[i];
int bit = (c>>j)&1;
parity ^= bit;
}
}
return parity;
}
这个函数返回一个很好的结果,下一个例子:
char* msg = malloc(sizeof(char*)*1400);
strcpy(msg,"some string");
int parity = parity(msg);
对于下一个示例,结果不好:
char* msg = malloc(sizeof(char*)*1400);
FILE *fp;
fp = fopen(filename,"r"); //filename is a binary file
while( !feof(fp) ){
fread(msg,1399,sizeof(char),fp);
int parity = parity(msg); //--> the result isn't well
//.......
}
当我从文件中读取时,我看到 strlen(msg) 在每个步骤(192,80,200...等)都是可变的。对于第二个示例,我必须更改“奇偶校验”功能。有什么建议么?