c++binarycharintbitset

error: cannot convert 'std::basic_string<char>' to 'char' in assignment


When I am compiling this code, I always get this error. Here dna is set to char. I want the bit value in the bitset to be converted to a char and the char to a int. If this is not the right way, please guide.

for (std::size_t i = 0; i < myString.size(); ++i)  
{
    std::bitset<8> y(myString[i]);

    dna = y.to_string<char>();
    binary = dna;
    cout << binary << " ";
    while ( binary >= 10) {
        for(int i = 0; i <= 100; ++i) { // loops length of array
            a[i] = binary % 10;
            binary = binary / 10;
        }
    }
}

Solution

  • y.to_string(); returns std::string. It can't be converted to char.

    Use this:

    std::string dna = y.to_string();
    unsigned long binary = y.to_ulong();