开发者

C++: how do I convert an unsigned char to a string representing the binary value that the char has?

开发者 https://www.devze.com 2023-02-20 07:45 出处:网络
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 eleg
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;
0

精彩评论

暂无评论...
验证码 换一张
取 消