Decimal conversions to base numbers 2-16 (Binary through Hexadecimal)

前端 未结 6 1627
夕颜
夕颜 2021-01-16 15:03

Hey i\'m writing a program that converts decimal numbers into any base unit from binary to hexadecimal (2,3,4,....,15,16). This is what I have so far, running any number fro

6条回答
  •  不要未来只要你来
    2021-01-16 15:38

    #include 
    void main(void) {
        char base_digits[16] =
            {'0', '1', '2', '3', '4', '5', '6', '7',
             '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
    
        char converted_number[32];
        long int number_to_convert;
        int base, index=0;
    
        printf("Enter number and desired base: ");
        scanf("%ld %i", &number_to_convert, &base);
    
        while (number_to_convert != 0)
        {
            converted_number[index] = base_digits[number_to_convert % base];
            number_to_convert = number_to_convert / base;
            ++index;
        }
        --index;
        printf("\n\nConverted Number = ");
        for(  ; index>=0; index--)
        {
            printf("%c", converted_number[index]);
        }
        printf("\n");
    }
    

提交回复
热议问题