Thread: String to Binary Conversion Problem

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    1

    String to Binary Conversion Problem

    I am 1st year student and i'm new to programming. Please Help for my problem. My program has no error already but the conversion always return 0 and no 1 at all. i.e. I save dog on the text pad then after i save it when i try to convert it always return all zero. It is very hard for me as beginner. Please Help me with this.

    insert
    Code:
    #include<iostream.h>
    #include<conio.h>
    #include<fstream.h>
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    
    fstream f;
    char ch;
    class ConSys{
      public:
        void headr();
        void intrfce();
        void view();
        void insert();
        int conBi();
        int conStr();
    };
    
    void main(){
      f.open("IO.TXT",ios::in);
      if(f.fail()) f.open("IO.TXT",ios::out);
      f.close();
      ConSys cs;
       while(1){
        clrscr();
        cs.headr();
        cs.intrfce();
        ch=getch();
        switch(ch){
          case 's': case 'S':
        clrscr();
        cs.headr();
        textcolor(BLUE);
        if(cs.conBi()==0){
          gotoxy(34,12);
          cputs("SUCCESSFUL!!!");
        }else{
          gotoxy(28,12);
          cputs("Invalid length of string");
        }
        gotoxy(80,25);
        getch();
        break;
          case 'b': case 'B':
        clrscr();
        cs.headr();
        textcolor(BLUE);
        if(cs.conStr()==0){
          gotoxy(34,12);
          cputs("SUCCESSFUL!!!");
        }else if(cs.conStr()==1 || cs.conStr()==2){
          gotoxy(28,12);
          cputs("Invalid length of string");
        }else{
          gotoxy(31,12);
          cputs("There is unwanted");
          gotoxy(29,13);
          cputs("character on text file");
        }
        gotoxy(80,25);
        getch();
        break;
          case 'v': case 'V':
        cs.view();
        getch();
        break;
          case 'i': case 'I':
        cs.insert();
        getch();
        break;
          case 'q': case 'Q':
        clrscr();
        cs.headr();
        textcolor(BLUE);
        gotoxy(35,12);
        cputs("Exiting...");
        getch();
        exit(0);
    
        }
    
      }
    }
    
    int ConSys::conStr(){  //Binary to String
      int i, oct=256, octInt=0, pos=0;
      char msgOut[2000], strIn[2000];
      for(i=0; i<2000; i++){
        msgOut[i]='\0';
        strIn[i]='\0';
      }
      f.open("IO.TXT",ios::in);
      f.getline(strIn,2000);
      f.close();
      for(i=0; i<=1600; i++){
        if(strIn[i] == '\0'){
          if(i%8==0) break;
          else return 2;
        }
        if(i==1600) return 1;
        if(strIn[i] != '0' && strIn[i] != '1') return 3;
      }
      for(i=0; i<1600; i++){
        if(strIn[i] == '\0') break;
        oct /= 2;
        if(strIn[i] == '1') octInt /= oct;
        if(oct == 1){
          msgOut[pos] = (char)(octInt);
          oct =256;
          octInt = 0;
          pos++;
        }
      }
      f.open("IO.TXT", ios::out);
      f<<msgOut;
      f.close();
      return 0;
    }
    
    int ConSys::conBi(){  //String to Binary
      int i=0, j, k, biInt=0, charInt, sumBi;
      char bi[2000], strIn[2000];
      for(i=0; i<2000; i++){
        bi[i]='\0';
        strIn[i]='\0';
      }
      f.open("IO.TXT",ios::in);
      f.getline(strIn,2000);
      f.close();
      for(i=0; i<=200; i++){
        if(strIn[i] == '\0') break;
        if(i==2000) return 1;
      }
      for(i=0; i<200; i++){
        if(strIn[i] == '\0') break;
        charInt = (int)(strIn[i]);
        sumBi = 128;
        for(j=0; j<8; j++){
          if(charInt >= sumBi){
        bi[biInt] = '1';
        charInt -= sumBi;
          }
          else bi[biInt] = '0';
          biInt++;
          sumBi / 2;
        }
      }
      f.open("IO.TXT", ios::out);
      f<<bi;
      f.close();
      return 0;
    }
    
    void ConSys::view()
    {
      char msgIn[1000];
      clrscr();
      headr();
      textcolor(YELLOW);
      gotoxy(37,6);
      cputs("MESSAGE");
      f.open("IO.TXT", ios::in);
      textcolor(GREEN);
      gotoxy(4,8);
      f.getline(msgIn, 1000);
      f.close(); cputs(msgIn);
      gotoxy(80,25);
    }
    
    void ConSys::insert(){
      char msgOut[300], c;
      int i;
      for(i=0; i<300; i++) msgOut[i]='\0';
      clrscr();
      headr();
      textcolor(BLUE);
      gotoxy(32,21);
      cputs("Your message must not more than 200 characters.");
      textcolor(WHITE);
      gotoxy(37,6);
      cputs("MESSAGE");
      gotoxy(4,8);
      i=0;
      while(i<200){
         c=getch();
         if(c==13){
           f.open("IO.TXT", ios::out);
           f<<msgOut;
           f.close();
           textcolor(BLUE);
           gotoxy(32,21);
           cputs("                                        SAVE!!!");
           gotoxy(80,25);
           return;
         }
         if(c == 8){
        if(i!=0){
           putch('\b');
           putch(NULL);
           putch('\b');
           --i;
           msgOut[i] = '\0';
           continue;
        }continue;
         }
         msgOut[i] = c;
         cout<<c;
         i++;
      }
      textcolor(BLUE);
      gotoxy(32,21);
      cputs("                Maximum length of string. SAVE!");
      f.open("IO.TXT", ios::out);
      f<<msgOut;
      f.close();
    }
    
    void ConSys::headr(){
      textcolor(BLUE+BLINK);
      gotoxy(31,2); cputs("CONVERSION SYSTEM");
      gotoxy(21,3); cputs("(String to Binary / Binary t String)");
      int i; textcolor(RED);
      gotoxy(3,4);
      for(i=0; i<76; i++) cputs("_");
      gotoxy(3,22);
      for(i=0; i<76; i++) cputs("_");
      textcolor(YELLOW);
      gotoxy(4,24);
      cputs("Name: COS 216");
      gotoxy(4,25);
      cputs("Section: BE1AA");
      gotoxy(52,24);
      cputs("Teacher: Ms. Jinky Tumasis");
      gotoxy(63,25);
      cputs("Subject: COS216");
    }
    
    void ConSys::intrfce(){
      textcolor(GREEN);
      gotoxy(15,8);  cputs("Conversion:");
      gotoxy(20,10); cputs("String to Binary");
      gotoxy(20,11); cputs("Binary to String");
      gotoxy(19,13); cputs("View content in text file (\"IO.TXT\")");
      gotoxy(19,14); cputs("Insert message in text file (\"IO.TXT\")");
      gotoxy(19,16); cputs("Quit");
      textcolor(YELLOW);
      gotoxy(16,10); cputs("[S]");
      gotoxy(16,11); cputs("[B]");
      gotoxy(15,13); cputs("[V]");
      gotoxy(15,14); cputs("[I]");
      gotoxy(15,16); cputs("[Q]");
      gotoxy(33,20); cputs("Choice:");cout<<ch;
    
    }
    Last edited by iamiel; 08-14-2013 at 09:56 AM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    you need to replace your compiler and book
    iostream.h is not only outdated, it is ancient

    I would get rid of conio.h and all its function - since it make your code not portable and most programmers will not be able to compile it on their compilers.

    And also you need to remove stdio.h - since it handles IO routines for C, and you are using <iostream> for that purposes in C++
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Built in Conversion from Integer to Binary String?
    By dleach in forum C Programming
    Replies: 20
    Last Post: 08-03-2013, 10:47 AM
  2. Problem with a binary to decimal conversion program. Help please.
    By Khalidkamalabdr in forum C++ Programming
    Replies: 11
    Last Post: 07-28-2013, 12:19 PM
  3. Decimal to binary conversion with character string
    By jbone0881 in forum C Programming
    Replies: 3
    Last Post: 02-06-2012, 12:09 PM
  4. String conversion problem
    By Mr.Bit in forum C Programming
    Replies: 9
    Last Post: 03-04-2008, 04:39 PM
  5. Problem regarding binary to char conversion
    By LuP in forum C Programming
    Replies: 1
    Last Post: 04-19-2002, 01:57 AM