C++: how do I convert an unsigned char to a string representing the binary value that the char has?
char myChar = 0x01;
how do I get my output to be "00000001"
If there is a way to do it wit开发者_开发知识库h a built in function, without a loop (I can do this with a loop, I just want to be elegant), that would be preferred ?
As for the why I'm doing this: I'm saving these numbers in a file that an external library needs this formatting (weird, I know, but libraries have rules).
You may use a bitset:
std::bitset<8> bits(myChar);
std::cout << bits << std::endl;
精彩评论