基本转换c

时间:2017-07-06 18:14:11

标签: c

我发现这个程序可以进行Base Conversions。以下行在下面的程序清单中做了什么?

printf("%c", base_digits[converted_number[index]]);

程序:

#include <stdio.h>

int main(void)
{
  char base_digits[16] =
   {'0', '1', '2', '3', '4','5', '6', '7','8', '9', 'A', 'B', 'C','D', 'E', 'F'};

  int converted_number[64];
  long int number_to_convert;
  int  base, index=0;

/* get the number and base */
 printf("Enter number and desired base: ");
 scanf("%ld %i", &number_to_convert, &base);

/* convert to the indicated base */
while (number_to_convert != 0)
{
 converted_number[index] = number_to_convert % base;
 number_to_convert = number_to_convert / base;
 ++index;
}

/* now print the result in reverse order */
--index;  /* back up to last entry in the array */
printf("\n\nConverted Number = ");
for(  ; index>=0; index--) /* go backward through array */
{
 printf("%c", base_digits[converted_number[index]]);
}
printf("\n");
return 0;
}

1 个答案:

答案 0 :(得分:0)

converted_number数组包含转换后的数字中每个数字的值。

base_digits数组包含与每个数字对应的字符。因此base_digits[converted_number[index]]为指定基数中的每个数字获取正确的字符。