I am trying to convert decimal to hexadecimal in C, but the code doesn't seem to be working, I think the problem is with the way I am printing the array.

I have tried changing variable names, but I am unsure about the current problem.

  int decNum = 0;
  int remainderDecHex = 0;
  int decHexQuotient[LENGTH_OF_STRING];
  char hexDecNum[LENGTH_OF_STRING];
  int sum = 0;
  int printNum = 0;
  int index = 0;

  while (userInputArray[index] != '\0' ) {
    decHexQuotient[index] = userInputArray[index];

    index ++;
    while ( decHexQuotient[index] != 0) {
      sum = sum + (decHexQuotient[index] %16);
      // Convert integers into characters

      if (sum < 10) {
        sum = sum + 48;
      } else {
        sum = sum + 55;
      }

      decHexQuotient[index] = decHexQuotient[index] + (decHexQuotient[index]/16);
      index ++;
    }

    printf("The hexadecimal Number is: ");

    for (printNum = printNum -1; printNum > 0; printNum --) {
      printf("%c",hexDecNum[printNum] );
    }

I would expect it to print the hexadecimal number, but it prints nothing, userInputArray is what I use to collect the information, it is a char array. At the top, are all of the variables and the logic for this code is that I am taking the user input as a string and converting it to a int, then checking to see if it is greater than 10 to add 48 to the ASCII code and the same in the else statement to change it to a-f for hexadecimal. The main problem seems to be that it is not printing out the way that I have the array printed.

Is this because I am printing the array incorrectly or because the code is not working?

2 Answers

0
P__J__ On
char *reverse(char *str)
{
    size_t len = strlen(str);
    char *end = str + len - 1;
    char *savedstr = str;
    char x;

    if(!str || !len) return str;

    while(end > str)
    {
        x = *end;
        *end-- = *str;
        *str++ = x;
    }
    return savedstr;
}

char tohex(long long x, char *buff)
{
    char digits[] = "01234567890ABCDEF";
    char *savedbuff = buff;
    char sign = 0; 
    if (x < 0)
    {
        sign = '-';
        x = -x;
    }

    do
    {
        *buff++ = digits[x & 0xf];
        x /= 16;
    }while(x);
    *buff++ = sign;
    if(sign) *buff = 0;
    return reverse(savedbuff);
}
0
Shopimo Online On
Would this work?

int decNum [LENGTH_OF_STRING];
                  int remainderDecHex = 0;
                  int decHexQuotient [LENGTH_OF_STRING];
                  char hexDecNum [LENGTH_OF_STRING];
                  int sum[LENGTH_OF_STRING];
                  int printNum = 0;
                  int j = 0;
                  int i = 0;

                  // Option #2 

                  if ( userInputArray[index] < 10 ) {



                  }


                  // Option #1

                  // Store decimal number into decNum 
                  while (userInputArray[index] != '\0' ) {

                    decNum[index] = userInputArray[index]; 


                    index ++;

                }
                  // make a copy and store the decimal number into decHexQuotient
                  while (decNum[index] != '\0') {

                      decHexQuotient[index] = decNum[index];


                      index++;

                  }



                 // find the remainder of decHexQuotient and store it into sum
                  while (decHexQuotient[index] != '\0'){

                      sum[index] = decHexQuotient[index] %16;


                    index++;

                  }
                    // if the sum is less than 10 and not equal to zero, add 48 ascii value 
                    if ( sum[index] < 10 && sum[index] != 0){

                        hexDecNum[j++] = 48 + sum[index];

                        index++;

                    }
                        // if not less than 10 add 55 ascii value 
                        else { hexDecNum[j++] = 55 + sum[index];

                        index++;
                        // divide the decimal number by 16 
                        while (decHexQuotient[index] != '\0'){

                            decHexQuotient[index] = decHexQuotient[index] / 16;

                            index++;

                        }

                         }
    // print out the hexadecimal number 
                  printf("\nThe Hexadecimal number is: %c", hexDecNum[i]);