help with c program: binary to ascii program
This is a program where i input a 8 digit binary character and it calculates binary to ascii so if i enter a binary character 01000001 the program calculate and give me the ascii 65 (which is a 'A')
My problem is, once i get 8 character i want to calculate the 8 and get the ascii then calculate the next set. Right now it calculate the last set i input.
ie. i enter 1010101011110000, itll do the calculations for 11110000 and not the first part which is 10101010
would i have to add code into the if statement?
Code:
int bit_to_ascii(const char core[ ], char data[ ]);
int main( ) {
char core2[256];
char data2[13];
int rc2;
printf("enter: ");
scanf("%256[^\n]", core2);
rc2 = bit_to_ascii(core2, data2);
printf("%d", rc2);
return 0;
}
int bit_to_ascii(const char core[ ], char data[ ]) {
int i, rc = 0, j, inte[7];
char binarray[9];
for(i=0, j=0; core[i] != '\0'; i++) {
switch(core[i]) {
case '1':
case '0':
if(j<8) {
binarray[j++] = core[i];
}
else {
j = 0; /* resets j to 0 after 8 1's or 0's have been found */
if(j<8) {
binarray[j++] = core[i];
}
}
break;
}
}
binarray[j] = '\0';
inte[0] = (binarray[0] - 48) * 128;
inte[1] = (binarray[1] - 48) * 64;
inte[2] = (binarray[2] - 48) * 32;
inte[3] = (binarray[3] - 48) * 16;
inte[4] = (binarray[4] - 48) * 8;
inte[5] = (binarray[5] - 48) * 4;
inte[6] = (binarray[6] - 48) * 2;
inte[7] = (binarray[7] - 48) * 1;
printf("the binary array is: '%s' %d %d %d\n", binarray, inte[0], inte[1], inte[2]);
return rc;
}