In: Computer Science
Write a program that receives an integer and must return a string containing the hexadecimal representation of that integer.
If you have any doubts, please give me comment...
//In this assignment, we write code to convert decimal integers into hexadecimal numbers
#include <iostream>
#include <string>
using namespace std;
//convert the decimal integer d to hexadecimal, the result is stored in hex
string dec_hex(int d)
{
char digits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int k = 0;
string hex = "";
while (d > 0)
{
int rem = d % 16;
hex = digits[rem]+hex;
d = d / 16;
k++;
}
return hex;
}
int main()
{
int d;
string hex;
cout<<"Enter an integer : ";
cin>>d;
hex = dec_hex(d);
cout<<"Decimal: "<<d<<", Hexadecimal: "<<hex<<endl;
return 0;
}