I have byte to binary string function,
std::string byte_to_binary(unsigned char byte)
{
int x = 128;
std::ostringstream oss;
oss << ((byte & 255) != 0);
for (int i = 0; i < 7; i++, x/=2)
oss << ((byte & x) != 0);
return oss.str();
}
How can i write an int to bits in same way? I don't want extra 0's at the beginning of binary string so that is why i can't figure out how to create a variable length each time. Also, i'm not using std::bitset.
std::bitset?bitsetoverloads them. Next reason?ostringstreamandstring, so you're clearly coding in C++. Use C++ classes for the stuff they're made for. Next reason? :P This may sound a bit harsh, but you'll be thankful later on.