0
string convert_binary_to_hex(string binary_value)
{
    bitset<8> set(binary_value);    
    cout << hex << set.to_ulong() << endl; // this is the output that I want to store to a variable to return
    return "";
}

I've not really done C before. =D

EDIT: a user suggested ostringstream:

ostringstream result;
bitset<8> set(binary_value);    
result << hex << set.to_ulong() << endl;
return result.str();

but it now give this error:

main.cpp:20: error: aggregate ‘std::ostringstream result’ has incomplete type and cannot be defined

Also, my imports:

#include <stdio.h>
#include <iostream>
#include <string>
#include <bitset>


using namespace std;
NullVoxPopuli
  • 56,640
  • 72
  • 202
  • 343

2 Answers2

3

Write it to an instance of std::ostringstream:

string convert_binary_to_hex(string binary_value)
{
    bitset<8> set(binary_value);    
    ostringstream oss;
    oss << hex << set.to_ulong() << endl; // this is the output that I want to store to a variable to return
    return oss.str();
}
NPE
  • 464,258
  • 100
  • 912
  • 987
0

See this answer:

C++ equivalent of sprintf?

And use the out variable like you're using cout here, and instead of

std::cout << out.str() << '\n';

you would just do

return out.str();
Community
  • 1
  • 1
Herms
  • 35,810
  • 12
  • 76
  • 101