C ++二进制到十进制 [英] C++ Binary to Decimal

查看:163
本文介绍了C ++二进制到十进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我知道有一种方法来转换使用十二月位集库为二进制,但你可以做同样的二进制使用位集库为十进制?或者你有做手工?

 位集和8; GT; bin_x(数);


解决方案

可以,但它应该是一个字符串。

 和std :: bitset< 8'比特(1111);
性病::法院LT&;< bits.to_ulong()&所述;&下;的std :: ENDL;

您可以将您的整数转换为字符串,然后将它传递给位集 C-器。

活生生的例子

So I know there is a way to convert Dec to binary using the bitset library but can you do the same for binary to decimal using the bitset library? or do you have to do it manually?

bitset<8> bin_x(number);

解决方案

You can, but it should be a string.

std::bitset<8> bits("1111");
std::cout << bits.to_ulong() << std::endl;

You can convert your integer to string and then pass it to bitset c-tor.

Live example

这篇关于C ++二进制到十进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆