Array-----binary number and decimal number interchange

Source: Internet
Author: User

  1. Binary number converted to decimal number
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <string.h>
  5. #include <process.h>
  6. using namespace Std;
  7. void Main () {
  8. int I, BITV, w=1, value = 0;//value cumulative sum, BITV bit value (0 or 1), W weight
  9. Char a[20];
  10. cout << "Enter a bit binary number (less than):" <<endl;
  11. Cin >> A;
  12. for (i = strlen (a)-1; I >= 0; i--) {
  13. BITV = A[i];
  14. if (Bitv = = ' 1 ')//Note string form
  15. Value + = W;
  16. Else
  17. if (bitv! = ' 0 ')
  18. {//Judging input is wrong
  19. cout << "Enter error!";
  20. Exit (0);
  21. }
  22. W *= 2; Replacement weight Value
  23. }
  24. cout << "-------------result-------------" << Endl;
  25. cout << a << "(2) =" <<value << "(Ten) <<endl;
  26. }
  27. -----------------------------------------------------------------------------------

Decimal number converted to binary number (successive except two, the remainder is inverted)

    1. #include "stdafx.h"
    2. #include <iostream>
    3. using namespace Std;
    4. void Main () {
    5. int n,bitv,i=0;
    6. int a[20];
    7. cout << "Please enter a integer ():" <<endl;
    8. CIN >> N;
    9. while (n >= 1) {
    10. BITV = n 2;
    11. A[i] = BITV;
    12. i++;
    13. n/=2;
    14. }
    15. cout << N << "(10) =";
    16. for (int j = i-1; J >= 0; j--) {
    17. cout << A[j];
    18. }
    19. cout << "(2)" <<endl;
    20. }

Array-----binary number and decimal number interchange

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.