how to convert decimal to hexadecimal in c++ code example
Example 1: convert decimal to binary in c++
#include <iostream>
using namespace std;
void decToBinary(int n)
{
int binaryNum[32];
int i = 0;
while (n > 0) {
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for (int j = i - 1; j >= 0; j--)
cout << binaryNum[j];
}
int main()
{
int n = 17;
decToBinary(n);
return 0;
}
Example 2: decimal to hex cpp
#include <iostream>
using namespace std;
int main(){
float i;
cout << "What is the number?: ";
cin >> i;
int* q = (int*)&i;
cout << hex << *q << endl;
return 0;
}