将int转换为基数2 cstring / string [英] Converting an int into a base 2 cstring/string
问题描述
在我的电脑上的visual studio我可以使用itoa()从一个基础十int转换为基础2 c字符串。但是当我在Linux机器上的功能不支持。还有另一种快速的方式做这种转换吗?我知道如何使用字符串流,我可以使用分割和修改手动转换到另一个基地。
In visual studio on my PC I can use itoa() to convert from a base ten int to a base 2 c-string. However when I am on a Linux machine that function isn't supported. Is there another quick way of doing this kind of conversion? I know how to use a string stream and I can use the dividing and modding to convert to another base manually.
我只是跳跃,有一个更简单的方法来访问int的二进制表示。
I was just hopping that there was an easier way of accessing the binary representation of an int.
推荐答案
您可以使用 N
()来使用
std :: bitset< N>
例如 std :: numeric_limits< int> :: digits
):
std::string bits = std::bitset<10>(value).to_string<std::string>();
请注意, int
。它们当然不是基数10,尽管这是格式化时使用的默认基数(使用 std :: oct
和 std :: hex
)。如果有什么, int
实际上用基数2表示。
Note that int
s just represent a value. They are certainly not base 10 although this is the default base used when formatting them (which can be easily change to octal or hexadecimal using std::oct
and std::hex
). If anything, int
s are actually represented using base 2.
这篇关于将int转换为基数2 cstring / string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!