我需要尽快将类似于unsigned int bin_number = 10101010
的二进制数转换为其十进制表示(即170
)?最好的算法是什么?
使用模板可以在编译时解决这个问题。
template<unsigned long num>
struct binary
{
static unsigned const value =
binary<num/10>::value << 1 | num % 10;
};
// Specialization for zero
template<>
struct binary<0>
{ static unsigned const value = 0; };
使用较小的num
再次实例化二进制模板,直到num
减少到零并以特化作为终止条件。
例如: std::cout << binary<10101010>::value;
对于运行时问题:
unsigned binary_to_decimal(unsigned num)
{
unsigned res = 0;
for(int i = 0; num > 0; ++i)
{
if((num % 10) == 1)
res += (1 << i);
num /= 10;
}
return res;
}
const double d = 170.0;
。由于他肯定在运行时获取入站号码,因此元编程已经不再适用了。 - John Diblingint i = 0b10101010;
即可,这是标准的:http://eel.is/c++draft/lex#nt:binary-literal - Martin Morterol如果这个"number"实际上是从某些来源获取的字符串(从文件或用户输入),你将其转换为数字(认为它更适合成为实际数字),这很可能会发生,那么你可以使用std :: bitset
进行转换:
#include <bitset>
unsigned int number = std::bitset<32>("10101010").to_ulong();
当然,这里的 32
是由实现定义的,可能更适合写成 std::numeric_limits<unsigned int>::digits
。#include <string>
unsigned int number = std::bitset<32>(std::to_string(bin_number)).to_ulong();
(使用C++11的to_string
)但这可能不再是最有效的方法,因为其他人提出了基于数字的更有效的算法。但如上所述,我怀疑您是否真的将此数字作为实际整数变量首先获得,而是从某些文本文件或用户那里读取。
实际上,如果你写unsigned int bin_number = 10101010
,编译器会将其解释为一个十进制数。
如果你想在源代码中写一个二进制字面量,应该使用BOOST_BINARY
。然后,你只需要使用cout
打印它,默认情况下是十进制...
unsigned int i = BOOST_BINARY(10101010);
std::cout << i; // This prints 170
i = 0b10101010
),但使用boost可以确保可移植性。 - Aurel自C++11以来(即使在这方面C++11比C++14更有限),函数可以是constexpr
,因此避免了需要template
来具有编译时值的必要性。
以下是与C++14兼容的版本:
constexpr unsigned binary_to_decimal(unsigned num)
{
unsigned res = 0;
while (num)
{
res = 10 * res + num % 10;
num /= 10;
}
return res;
}
对于字面量,自C++14起,您甚至可以使用二进制字面量:
0b1010'1010 // or 0b10101010 without separator
如果您知道您正在处理的二进制位数,而且它始终是固定的,并且二进制数字以字符串形式出现(如果从文件或stdin读取,则会这样),则可以采用以下方法:
int to_binary( const char* c )
{
return ( ( c[0] & 1 ) ? 0x80 : 0x00 ) |
( ( c[1] & 1 ) ? 0x40 : 0x00 ) |
( ( c[2] & 1 ) ? 0x20 : 0x00 ) |
( ( c[3] & 1 ) ? 0x10 : 0x00 ) |
( ( c[4] & 1 ) ? 0x08 : 0x00 ) |
( ( c[5] & 1 ) ? 0x04 : 0x00 ) |
( ( c[6] & 1 ) ? 0x02 : 0x00 ) |
( ( c[7] & 1 ) ? 0x01 : 0x00 );
}
这假设有一个固定的八位二进制数,被称为:
std::cout << to_binary("10101010") << std::endl;
如果你有一个十六位的数字,你仍然可以使用它:
const char* bin_number = "1010101010101010";
// Deal with 16 bits
std::cout << ( to_binary( bin_number ) << 8 | to_binary( bin_number + 8 ) ) << std::endl;
10101010
是来自于你程序的用户,还是只是代码中的字面量? - Michael Kristofik