Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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);
return 0;
}
int bit_to_ascii(const char core[ ], char data[ ]) {
int i, rc = 0, j, inte[7], len, len2, m, lenrpt, rpt1, rptcounter, bin, lendata = 0, p = 0;
char binarray[9], rpt[3], joe[256];
for(i=0, j=0; core[i] != '\0'; i++) {//loops until end of string
switch(core[i]) {
case '1':
case '0':
if(j<8) {
binarray[j++] = core[i];//puts 1 or 0 in the variable binarray
}
else {
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;
bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];//calculates binarray to decimal
data[lendata] = bin;
lendata++;
printf("the binary array is: '%s' %d %d %d %d %d %d %d %d\n", binarray, inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
j = 0; /* resets j to 0 after 8 1's or 0's have been found */
if(j<8) {
binarray[j++] = core[i];
}//end if
}//end else
case 'R':
if(core[i+1] == 'P' && core[i+2] == 'T') {//checks if the next 2 characters are PT to make RPT
if(core[i+3]>='0' || core[i+3]<='9') {
rpt[0] = core[i+3];//get number after RPT
if(core[i+4]>='0' || core[i+4]<='9') {
rpt[1] = core[i+4];
if(core[i+5]>='0' || core[i+5]<='9') {
rpt[2] = core[i+5];
}
}
lenrpt = strlen(rpt);//gets lenght of number after rpt
i = i + lenrpt;
rpt[3] = '\0';
rpt1 = atoi (rpt);//uses string rpt to put in rpt1 as a integer
for(rptcounter=1; rptcounter!=rpt1; rptcounter++){
len = strlen(binarray);
if(len<8){
len2 = 8 - len;
m = len;
do {
binarray[m]='0';
m++;
}while (m != 8);
}
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;
bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];
data[lendata] = bin;
lendata++;
printf("the binary array is: '%s' %d %d %d %d %d %d %d %d\n", binarray, inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
}
}
}
break;
case 'S':
if(core[i+1] == 'K' && core[i+2] == 'P') {//check for KP to make SKP
i = i + 3;
do {//loops 8 times to skip 8 1's/0's
switch(core[i]) {
case '1':
case '0':
p++;
i++;
break;
}
} while (p<7);
}
break;
}//end case
}//end for
binarray[j] = '\0';
len = strlen(binarray);
if(len<8){
len2 = 8 - len;
m = len;
do {
binarray[m]='0';
m++;
}while (m != 8);
}
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;
bin = inte[0] + inte[1] + inte[2] + inte[3] + inte[4] + inte[5] + inte[6] + inte[7];
data[lendata] = bin;
lendata++;
data[lendata]='\0';
printf("the binary array is: '%s' %d %d %d %d %d %d %d %d\n", binarray, inte[0], inte[1], inte[2], inte[3], inte[4], inte[5], inte[6], inte[7]);
printf("\nBit Converted to ASCII is: %s\n", data);//display the ascii
rc = strlen(data);//find the lenght of the cahr data
return rc;
}