Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- void Input(int &number)
- {
- do
- {
- cout << "\nEnter number : ";
- cin >> number;
- if (number < 0)
- cout << "Xin moi ban nhap lai : " << endl;
- } while (number < 0);
- }
- void BinaryToHexadecimal(int number)
- {
- int hexa[32] = { 0 }, dec = 0;
- int i = 1, j, rem;
- //binary to decimal
- while (number > 0)
- {
- rem = number % 2;
- dec = dec + (rem * i);
- number /= 10;
- i *= 2;
- }
- //decimal to hexa
- i = 0;
- while (dec != 0)
- {
- hexa[i] = dec % 16;
- dec /= 16;
- i++;
- }
- cout << "Hexa Decimal valua: ";
- for (j = i - 1; j >= 0; j--)
- {
- if (hexa[j] > 9)
- cout << char(hexa[j] + 55);
- else
- cout << hexa[j];
- }
- cout << endl;
- system("pause");
- return;
- }
- int main()
- {
- int number;
- Input(number);
- BinaryToHexadecimal(number);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement