赞
踩
Multiply each digit from right to left by power of 2. Here the power of 2 will be the position of the digit starting from 0.
从右到左的每个数字乘以2的幂。这里2的幂是从0开始的数字的位置。
Now add all the values to obtain decimal number.
现在将所有值相加以获得十进制数。
- #include<iostream>
- #include<math.h>
-
- using namespace std;
-
- int main()
- {
- unsigned long i,n,num=0,d;
- cout<<"Enter any Binary number:";
- cin>>n;
- cout<<"\nThe Decimal conversion of "<<n<<" is ";
-
- for(i=0;n!=0;++i)
- {
- d=n%10;
- num=(d)*(pow(2,i))+num;
- n=n/10;
- }
-
- cout<<num;
- return 0;
- }
翻译自: https://www.thecrazyprogrammer.com/2011/03/c-program-to-convert-binary-number-to.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。