在C中将十进制转换为十六进制并打印数组

时间:2019-04-13 16:16:57

标签: c hex decimal

我正在尝试在C中将十进制转换为十六进制,但是代码似乎无法正常工作,我认为问题出在我打印数组的方式上。

我尝试过更改变量名,但是不确定当前的问题。

  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] );
    }

我希望它显示十六进制数字,但不显示任何内容,userInputArray是我用来收集信息的内容,它是一个char数组。最上面是所有变量,此代码的逻辑是,我将用户输入作为字符串并将其转换为int,然后检查是否将ASCII代码添加48大于10。与else语句中的相同,将其更改为十六进制的af。主要问题似乎是它没有像我打印数组那样打印出来。

这是因为我不正确地打印阵列还是因为代码无法正常工作?

2 个答案:

答案 0 :(得分:0)

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);
}

答案 1 :(得分:0)

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]);