How to convert ASCII string to binary string C++

1 Answer

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

std::string StringToBinary(std::string s) {
    std::string binary = "";
    for (char &ch : s) {
        binary += std::bitset<8>(ch).to_string();
    }
    return binary;
}

int main()
{
    std::string s = "c++"; 
 
    std::cout << StringToBinary(s) << "\n";

   return 0;
}



/*
run:

011000110010101100101011

*/

 



answered Aug 2, 2020 by avibootz

Related questions

1 answer 210 views
1 answer 210 views
1 answer 129 views
1 answer 193 views
1 answer 105 views
1 answer 194 views
1 answer 206 views
206 views asked Nov 27, 2019 by avibootz
...