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 ...など)で変数であることがわかりました。2 番目の例では、「パリティ」関数を変更する必要があります。助言がありますか?