binary code

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

Header.h
1
2
3
#include <iostream>
#include <string>
#include <cctype> 


Source.cpp
1
2
3
4
5
6
7
8
9
10
11
12
#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
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
#include <iostream>
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.