Friday 20 June 2014

C++ program to convert character string to binary


Here is a C++ program to convert character string to binary


 SOURCE CODE OUTPUT
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
    char str[20];
    long rem,temp=1,decimal,binary=0;
    cout<<"Enter the character string : ";
    gets(str);
    cout<<"Equivalent Binary string : ";
    for(int i=0;str[i]!='\0';i++)
    {
       decimal=int(str[i]);
       while(decimal)
       {
          rem=decimal%2;
          decimal/=2;
          binary+=rem*temp;
          temp*=10;
       }
       temp=1;
       cout<<binary<<" ";
       binary=0;
    }
    return 0;
}

Any questions regarding to program please write in comments.

No comments:

Post a Comment