How to convert text to binary code in C++

1 Answer

0 votes
#include <iostream>
#include <bitset>
#include <sstream>
#include <string>

std::string text2bin(const std::string &txt) {
    std::ostringstream bin;

    for (char ch : txt) {
        bin << std::bitset<8>(ch) << " ";
    }

    return bin.str();
}

int main() {
    std::string txt = "C++ Programming";
    
    std::string bin = text2bin(txt);
    
    std::cout << bin << std::endl;
}



/*
run:

01000011 00101011 00101011 00100000 01010000 01110010 01101111 01100111 01110010 01100001 01101101 01101101 01101001 01101110 01100111 

*/

 



answered Nov 17, 2024 by avibootz

Related questions

1 answer 92 views
1 answer 100 views
1 answer 112 views
112 views asked Nov 17, 2024 by avibootz
1 answer 100 views
1 answer 100 views
1 answer 109 views
1 answer 109 views
...