簡體   English   中英

在C中將十進制轉換為十六進制並打印數組

[英]Converting Decimal to Hexadecimal in C and printing Arrays

我試圖在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為十六進制。 主要問題似乎是它沒有打印出我打印陣列的方式。

這是因為我打印錯誤的數組或因為代碼不起作用?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM