# Thread: Help: Bin to ASCII program

1. ## Help: Bin to ASCII program

what the program does is search through the string core looking for 1's and 0's and every 8 1's and 0's it converts it to decimal (01010000 is 80 as a decimal, so 80 in the string variable data is a 'P')

but also it looks for RPT with a number after RPT (ie. RPT2) the number means how much times to repeat the binary (ie. 01010000 RPT2 means there will be two 'P's because 01010000 is a 80 a decimal 80 is 'P' in ascii)

then there is SKP, which when found skips the next 8 binary characters (ie. 01010000 SKP 00110000 means 00110000 will not get calculated)

well i got the code down for all of this but i think i have some logic error because when i put 01001111RPT00201010000 i get 01001111 two time because RPT002 but i dont get 01010000. If someone check just chekc out my code and tell me what i should add or take out or modify that would be nice.
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("&#37;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;
}```

2. I don't like this line one little bit:
Code:
`            lenrpt = strlen(rpt);//gets lenght of number after rpt`
especially just after I watched you put three characters in a 3-character array. And then after that:
Code:
`             rpt[3] = '\0';`
Tsk tsk.

If you want to use that strlen, make sure (1) you have enough space, and (2) you have \0 filled in your array already, so that you can find the end of the string later.

3. should i up the rpt index by 1? (ie. rpt[4])

4. That would take care of (1). You would still need to do (2).

5. rpt[4] = '\0' after declaring it?

6. Originally Posted by bigmac(rexdale)
rpt[4] = '\0' after declaring it?
Best thing would be to do
Code:
`rpt[4] = {0};`
as that guarantees that the whole array is filled with zeros. Just bear in mind that if you attempt to re-use the same variable, you'll also need to re-fill the string, something like:
Code:
`memset(rpt, 0, sizeof(rpt));`
--
Mats

7. ok guys i found out the problem

i = i + lenrpt;

^^it had no propose so i decided to comment it out and the program worked