- Binary number converted to decimal number
- #include "stdafx.h"
- #include <iostream>
- #include <string.h>
- #include <process.h>
- using namespace Std;
- void Main () {
- int I, BITV, w=1, value = 0;//value cumulative sum, BITV bit value (0 or 1), W weight
- Char a[20];
- cout << "Enter a bit binary number (less than):" <<endl;
- Cin >> A;
- for (i = strlen (a)-1; I >= 0; i--) {
- BITV = A[i];
- if (Bitv = = ' 1 ')//Note string form
- Value + = W;
- Else
- if (bitv! = ' 0 ')
- {//Judging input is wrong
- cout << "Enter error!";
- Exit (0);
- }
- W *= 2; Replacement weight Value
- }
- cout << "-------------result-------------" << Endl;
- cout << a << "(2) =" <<value << "(Ten) <<endl;
- }
- -----------------------------------------------------------------------------------
Decimal number converted to binary number (successive except two, the remainder is inverted)
- #include "stdafx.h"
- #include <iostream>
- using namespace Std;
- void Main () {
- int n,bitv,i=0;
- int a[20];
- cout << "Please enter a integer ():" <<endl;
- CIN >> N;
- while (n >= 1) {
- BITV = n 2;
- A[i] = BITV;
- i++;
- n/=2;
- }
- cout << N << "(10) =";
- for (int j = i-1; J >= 0; j--) {
- cout << A[j];
- }
- cout << "(2)" <<endl;
- }
Array-----binary number and decimal number interchange