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 33 34 35 36 37 38
|
#include <string>
#include <cctype>
#include <iostream>
#include <unordered_map>
std::unordered_map<char, std::string> substitute_for =
{
{ 'A', "100 0001" }, { 'B', "100 0010" }, { 'C', "100 0011" }, { 'D', "100 0100" },
{ 'E', "100 0101" }, { 'F', "100 0110" }, { 'G', "100 0111" }, { 'H', "100 1000" },
{ 'I', "100 1001" }, { 'J', "100 1010" }, { 'K', "100 1011" }, { 'L', "100 1100" },
{ 'M', "100 1101" }, { 'N', "100 1110" }, { 'O', "100 1111" }, { 'P', "101 0000" },
{ 'Q', "101 0001" }, { 'R', "101 0010" }, { 'S', "101 0011" }, { 'T', "101 0100" },
{ 'U', "101 0101" }, { 'V', "101 0110" }, { 'W', "101 0111" }, { 'X', "101 1000" },
{ 'Y', "101 1001" }, { 'Z', "101 1010" }
};
std::string convert(const std::string& text)
{
std::string result;
for (auto ch : text)
{
if (std::isalpha(ch)) result += substitute_for[std::toupper(ch)];
else result += ch;
}
return result;
}
int main()
{
std::string text;
std::cout << "Enter statement you would like to convert to binary: ";
std::getline(std::cin, text);
std::cout << convert(text) << '\n';
}
|