Error in c++ stringstream write function

Asked

Viewed 199 times

0

Expensive,

I am doing a function for decimal to binary conversion, which is returned in a string. As C++ does not have StringBuffer, I’m using the stringstream. However, I always have build error when using the function write of stringstream. The code is this:

string toBin(int valor, int bits) {
    int resto = -1, i = 0;
    stringstream ss;

    if (valor == 0) {
        return "0";
    }

    while (valor > 0) {
        resto = valor % 2;
        valor = valor / 2;
        ss.seekp(0);
        ss.write(resto, i++);
    }

    return ss.str();
}

The intention is that with each rest found, I insert it in the first position of the stringstream, to form the binary. And the error found is:

"invalid Conversion from 'int' to 'const char*'

How could I solve this problem? Or is there a C++ library that already does the decimal to binary conversion?

Grateful!

2 answers

3


How could I solve this problem?

The function write accepted as a parameter const char* and you are trying to pass an integer number. Well, compilation problems would be solved if you convert the integer resto for a const char* with something like:

ss.write(std::to_string(resto).c_str(), i++);

But it seems to me that even with this conversion, your program still wouldn’t do what you expect of it and would need to fix more things.

Or is there a C++ library that already does the decimal to binary conversion?

Yes, there is a library that does this conversion. You can use the class std::bitset. Her builder gets the integer you want to convert to binary. And to convert binary to std::string you could use the function to_string. Your job would be like this:

#include <bitset>


std::string toBin(int valor){
    return std::bitset<32>(valor).to_string();
}

Detail: the literal 32 represents the number of bits manipulated by std::bitset, you can change this value according to your need, but this value needs to be known at compile time.

  • Perfect answer! With bitset I won’t need to handle the amount of return bits. Thank you very much!

0

That code is unnecessarily complicated.
Below a very simple solution.

#include <iostream>
#include <string>

using namespace std;

static string toBin(int valor)
{
  // quantidade de bits num int
  int nBits = sizeof(int) * 8;

  // string que vai conter o valor em binario
  string result;

  for (int i = 0; i < nBits; i++)
  {
    // testa o ultimo bit 'a direita se e' 1 ou 0
    if (valor & 1)
      result.insert(0, 1, '1'); // insere '1' no inicio da string
    else
      result.insert(0, 1, '0'); // insere '0' no inicio da string

    // desloca o valor 1 bit 'a direita
    valor >>= 1; // valor = valor >> 1;
  }

  return result;
}

int main()
{
  cout << "*\n";
  cout << "* 10="  << toBin(10)  << "\n";
  cout << "* 16="  << toBin(16)  << "\n";
  cout << "* 255=" << toBin(255) << "\n";
  cout << "*\n";
}
  • Thanks for the help! I ended up using the solution that v.Santos suggested, as it is a native feature of C++. Anyway, this solution of yours is very interesting too. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.