Guest_imported
New member
- Jan 1, 1970
- 0
How do I display a decimal number and the corresponding hexadecimal value?
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
#include <iostream.h>
const char values[ 20 ] = "0123456789ABCDEF";
void convertToHex( char *str, int p , int pos )
{
int a = p % 16;
if ( p > 0 )
{
str[ pos ] = values[ a ];
convertToHex( str , p / 16 , pos - 1 );
}
}
void convertToHex( char *str, int p )
{
int a = 0 , b;
b = p;
while ( b > 15 )
{
b = b / 16;
a = a + 1;
}
str[ a + 1 ] = '\0';
convertToHex( str , p , a );
}
int main()
{
int amount;
char buf[ 80 ] = "";
cout << "Enter a value: ";
cin >> amount;
convertToHex( buf , amount );
cout << buf << '\n';
return 0;
}