如何在 C++ 中将 int 转换为二进制字符串表示形式

2022-01-09 00:00:00 string binary g++ c++

我有一个 int,我想将其存储为二进制字符串表示形式.如何做到这一点?

I have an int that I want to store as a binary string representation. How can this be done?

推荐答案

试试这个:

#include <bitset>
#include <iostream>
int main()
{
    std::bitset<32>      x(23456);
    std::cout << x << "
";


    // If you don't want a variable just create a temporary.
    std::cout << std::bitset<32>(23456) << "
";
}

相关文章