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

问题描述 投票:0回答:2

我试图在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,然后检查它是否大于10以向ASCII代码添加48并且在else语句中将其更改为af为十六进制。主要问题似乎是它没有打印出我打印阵列的方式。

这是因为我打印错误的数组或因为代码不起作用?

c hex decimal
2个回答
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);
}

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]);
© www.soinside.com 2019 - 2024. All rights reserved.