ASCII to Binary Convertion

Im still messed up with the code i'm having now. I dont know whats wrong with the my code. when I used cout inorder to show to binary format like 00001001 10101100 etc it shows correctly but when im trying to save it on a file it it only save an 8 bit binary like 10001010.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
 int ConSys::conBi(){  //String to Binary
  int i=0, j, k,len, ascii,binary[9], charInt,total;

  char letter,bi[2000], strIn[2000];
  for(i=0; i<=200; i++){
    bi[i]='\0';
    strIn[i]='\0';
  }
  f.open("IO.txt",ios::in);
  f.getline(strIn,2000);
  f.close();
  for(i=0; i<=2000; i++){
    if(strIn[i] == '\0') break;
  len = strlen(strIn);

    for(j=0; j<=len; j++)
   {
      total = 0;
      letter = strIn[i]; /* store the first letter */
      ascii = (int)(letter);    /* put that letter into an int, so we can
			    see its ASCII number */
      while(ascii>0) /* This while loop converts the ASCII # into binary,
			stores it backwards into the binary array. */
      {
	 if((ascii%2)==0)
	 {
	    binary[total] = 0;
	    ascii = ascii/2;
	    total++; /* increasing by one each time will yeild the
			number of numbers in the array. */
	 }
	 else
	 {
	    binary[total] = 1;
	    ascii = ascii/2;
	    total++;
	 }
	 }
      total--;
     }
     f.open("IO.txt",ios::out);
      while(total>=0)
      {
	cout<<binary[total];
       f<<binary[total]; 'When i view the saved Binary on the text file it only show the last binary of the character. id doesn't show the other character.
       total--;
  }
   }
  f.close();
  return 0;
}



Hope you understand my code. im freshmen student only. Please have some time to look and correct the problem.
why don't you use C++ strings over C strings

eg: string name;

Aceix.
Topic archived. No new replies allowed.