Fast way to convert a binary number to a decimal number

前端 未结 5 1705
感情败类
感情败类 2021-02-05 17:29

I have to convert a binary number like for example unsigned int bin_number = 10101010 into its decimal representation (i.e. 170) as quickly as possible

5条回答
  •  我寻月下人不归
    2021-02-05 18:22

    Actually if you write unsigned int bin_number = 10101010, this is interpreted as a decimal number by the compiler.

    If you want to write a binary literal in your source code, you should use BOOST_BINARY. Then, you just need to print it using cout, decimal is the default...

    unsigned int i = BOOST_BINARY(10101010);
    std::cout << i; // This prints 170
    

提交回复
热议问题