Your requirements seem to keep changing. Watch and learn:
#include <iostream>
using namespace std;
// You MUST accept a string as the argument
int Convert_to_Dec ( const char *hex );
// You MUST return a string
char *Convert_to_Hex ( int dec );
int main()
{
cout<< Convert_to_Dec ( "1B" ) <<endl;
cout<< Convert_to_Hex ( 27 ) <<endl;
}
int Convert_to_Dec ( const char *hex )
{
int ret = 0;
while ( *hex != '\0' ) {
if ( *hex >= '0' && *hex <= '9' )
ret = 10 * ret + ( *hex - '0' );
else {
// Assuming ASCII
ret = 16 * ret + ( *hex - 'A' + 10 );
}
++hex;
}
return ret;
}
char *Convert_to_Hex ( int dec )
{
static char hex[20];
int i = 0, j = 0;
// Build the string
while ( dec != 0 ) {
hex[i++] = "0123456789ABCDEF"[dec % 16];
dec /= 16;
}
// Make it a valid string
hex[i] = '\0';
// Reverse the string
while ( j < i ) {
char temp = hex[j];
hex[j++] = hex[--i];
hex[i] = temp;
}
return hex;
} If this isn't what you want then you'll need to specify what you're doing. Are these functions converting a single digit? Because that's the only way you can avoid using strings and arrays.