How do you convert back a binary to string?
std::string test1("Hello");
std::bitset<8> test2;
test2 = std::bitset<8>(test1.c_str()[0]);
std::cout << test2 << std::endl;
std::string test3=test2.to_string<char,std::char_traits<char>,std::allocator<char> >();
std::cout << test3 << std::endl;
This will output:
01001000
01001000
How do i output back “Hello”?
Your
std::bitset<8>clearly can’t hold the world"Hello". After all, you are only representing 8 bits. What you can do is to convert eachcharto astd::bitset<8>(assumingcharis 8 bit, of course, which is typically the case but not guaranteed) and thestd::bitset<8>back to its value as an integer usingto_ulong(). Converting this value thus obtained tocharshould yield the original character:To get
"Hello"back you’d need a biggerstd::bitset<N>and you’d need to make sure you extract the correct bits to reassemble the correct individual character.