is there a way to convert everything i input to my program into its ascii binary equivalent? if so please show me; thanks.

Recommended Answers

All 4 Replies

I wish you had written the language which you are using...
I wrote it in C but you can easily integrate it on C++...

#include <stdio.h>
#include <math.h>

int main()
{
    char ch;//input character
    int ascii;//input's ascii equivalent
    int b,i;
    
    double sum = 0;//You can initilaize it to int but some compilers(like Dev-C++) causes troubles  and generates wrong output...
    
    printf("Enter any character ");
    scanf(" %c",&ch);
    
    ascii = ch;//if you asssign a char to an int, the int variable holds the assigned character's ASCII code
    
  
    
    for(i = 0;; i++)
    {
          b = ascii % 2;//converting binary...
          ascii /= 2;//converting binary...
          
          sum += b * pow(10,i);//variable b's first value is first digit the binary code from right to left
          //so we multiply it by E+0
          printf("%0.lf\n",sum);//Watch how your variable to be converted in ASCII
          
          if(ascii == 1 || ascii == 0) break;//If we divide an integer to 2, we obtain 1 or 0 as a result
          //if we don't control this will be an infinite loop.
    
          
    }
    
    sum += ascii * pow(10,i + 1);//End of our loop we divided the value 2 again and the variable ascii's value was 1
    //so we exit the loop beacause of the if statement and we didn't multiply it E + 6.We are doing this here. 
    
    printf("%0.lf",sum);//Output...
    
    
    
    return 0;
    
}

im sorry for not putting the language. i program in c++, thanks for the code.

im sorry for not putting the language. i program in c++, thanks for the code.

Not that it matters...
As I told my previous message , you can easily integrate this code on C++...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.