### binary code

how would i replace cirtain letters in a string that was inputed with somthing else such as the code for binary code

 ``123`` ``````#include #include #include ``````

Source.cpp
 ``123456789101112`` ``````#include "Header.h" using namespace std; int main() { string input; cout << "Enter statement you would like to convert to binary: "; getline(cin >> ws, input); }``````
If this is homework you need to post your attempts or thought process on this...

I think a better (but not really useful answer to your question) would be why in the heck anyone would convert a string to binary. Hex would make much more sense.

My thought process on how you COULD do this... get the ASCII value of each individual character in the string. With the ASCII value, you can easily convert the character to any base system with relative ease. Do a google search on decimal to binary code.
This is not homework and this is my attempt I don't know how to replace character with the binary code that's why I posted it on here so can someone give me some useful information on how I would go about this it would be much appreciated.
Last edited on
Hi, review the function itoa(...);

http://www.cplusplus.com/reference/cstdlib/itoa/
Last edited on
This doesn't help I'm trying to convert letters into binary
 ``1234567891011121314151617181920212223242526272829303132`` ``````#include using namespace std; void convertBin(int); void main() { char letter; cout << "Please enter a character: "; cin >> letter; if (!isalpha(letter)) cout << "This is not an alpha character."; else { cout << letter << " converted to binary is: "; convertBin(letter); cout << endl; } } void convertBin(int num) { int remainder; if (num <= 1) { cout << num; return; } remainder = num % 2; convertBin(num >> 1); cout << remainder; } `````` ``` Please enter a character: a a converted to binary is: 1100001 ```
Topic archived. No new replies allowed.