Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void Input(int &number)
  5. {
  6. do
  7. {
  8. cout << "\nEnter number : ";
  9. cin >> number;
  10. if (number < 0)
  11. cout << "Xin moi ban nhap lai : " << endl;
  12. } while (number < 0);
  13. }
  14. void BinaryToHexadecimal(int number)
  15. {
  16. int hexa[32] = { 0 }, dec = 0;
  17. int i = 1, j, rem;
  18. //binary to decimal
  19. while (number > 0)
  20. {
  21. rem = number % 2;
  22. dec = dec + (rem * i);
  23. number /= 10;
  24. i *= 2;
  25. }
  26. //decimal to hexa
  27. i = 0;
  28. while (dec != 0)
  29. {
  30. hexa[i] = dec % 16;
  31. dec /= 16;
  32. i++;
  33. }
  34. cout << "Hexa Decimal valua: ";
  35. for (j = i - 1; j >= 0; j--)
  36. {
  37. if (hexa[j] > 9)
  38. cout << char(hexa[j] + 55);
  39. else
  40. cout << hexa[j];
  41. }
  42. cout << endl;
  43. system("pause");
  44. return;
  45. }
  46. int main()
  47. {
  48. int number;
  49. Input(number);
  50. BinaryToHexadecimal(number);
  51. system("pause");
  52. return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement