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

前端 未结 6 1477
夕颜
夕颜 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

    This is weird, you return 0, which means "everything went fine" when there is an error, and you won't return anything otherwise. Plus yea, you meant || not | You didn't initialize your z variable, it could never enter the loop if it is 0, and even worse, you are never affection new value to z, so if it is not 0, it will never be and you are in an infinite loop

    0 讨论(0)
  • #include <stdio.h>
    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");
    }
    
    0 讨论(0)
  • 2021-01-16 15:42

    Um introduction to an old style debugging process called dry run

    x  y  z c (z != 0) 
    16 2  8 0  true
          8 0  true DOH!
    
    0 讨论(0)
  • 2021-01-16 15:50

    This is fun. Some suggestions:

    • use descriptive variable names (base, remainder, digit, number, etc)
    • dont cheat, using %X is just wrong
    • how about supporting duodecadecimal (sp?), base 36?
    • print out useful symbols (letters?) for digit symbols larger than 9
    • consider termination conditions, you weren't changing z (remainder), thus your loop
    • you were printing out the number in reverse order, right to left instead of left to right

    This version does up to base 36, printing letters for digit symbols > 9, and prints both reversed and expected order...

    #include <stdio.h>
    #include <stdlib.h>
    
    char BASIFICATION[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int MAXBASE=36;
    int main(void)
    {
      int done=0;
      long num, base, remainder, digit;
    
      printf("Number Base Convertificator\n");
      printf("(enter 0 0 when done)\n");
      while( !done)
      {
        printf("Please enter two integers: ");
        scanf("%d", &num);
        scanf("%d", &base);
        if( (base<2) || (base>MAXBASE) ) //avoid precedence until you are comfortable/confident with it
        {
          printf("Please enter base between 2 and %d.\n",MAXBASE);
          if(base<=0) done=1;
          continue;
        }
        printf("%d\n", num);
        printf("%d\n", base);
        printf("\n");
        if(base == MAXBASE) //cheaters never win!
        {
          printf("%XX\n", num);
          continue;
        }
        //if(base > 1 && base < MAXBASE)
        {
          char reversed[99], ndx=0;
          //this prints your digits in reverse order
          remainder = num;
          while(remainder > 0) //numerical methods, avoid skipping below zero (irrelevant here, but good habit)
          {
             digit = (remainder%base);
             remainder = (remainder/base);
             printf("%c ", BASIFICATION[digit]);
             //printf("%c %d (%d)\n", BASIFICATION[digit], digit, remainder);
             reversed[ndx++] = BASIFICATION[digit];
          }
          printf("\n");
          //reverse digits to print in expected order
          for( ; ndx>0; ) { printf("%c ",reversed[--ndx]); }
          printf("\n");
        }
      }
    }
    

    And when run,

    Number Base Convertificator
    (enter 0 0 when done)
    Please enter two integers: 1020 2
    1020
    2
    
    0 0 1 1 1 1 1 1 1 1 
    1 1 1 1 1 1 1 1 0 0 
    Please enter two integers: 252 16
    252
    16
    
    C F 
    F C 
    Please enter two integers: 99 
    8
    99
    8
    
    3 4 1 
    1 4 3 
    

    Now, how would you modify this to convert an input string into a specified base?

    0 讨论(0)
  • 2021-01-16 15:59

    Since x is not modified in this loop, z is always calculated the same value, and the loop never exits:

       while(z != 0){
       z = (x/y);
       c = (x%y);
       printf("%d\n", z);
       }
    

    To convert a number to a difference base, what you usually do is repeatedly divide the number by the base, and the remainders give you the digits. This will print out the digits from last to first:

       while(x != 0){
          c = (x%y); /* remainder of x when divided by y */
          x = (x/y); /* x divided by y */
          printf("%d\n", c);
       }
    
    0 讨论(0)
  • The purpose of this program is to learn how to convert numbers to other bases, not just to try out printf when it gives you a shortcut. Why not try:

    if (y > 1 && y <= 16) {
         parsedNumber = convert(x, y);
    } else {
         reportIllegalBase(y);
    }
    

    with appropriate methods?

    Also, don't use x and y for variable names. You are writing this for understanding, not to play Code Golf.

    You should also try to make this a more general purpose program. Instead of printing everything as you calculate, construct a string (a char[]) and print that. So, the convert function I suggested you write should be declared as:

    char[] convert(const int number, const int base) {...}
    

    or

    void convert(int * const buffer, const int number, const int base) {...}
    

    By the way, what happens if x is negative? I'm not sure whether you do the right thing. You might want to get x from the user first, and then get y from the user. That way you can give the user a second chance if he types in 1 instead of 16:

    int getBase() {
        int base;
        while(true) {
            printf("Please type in the base: ");
            scanf("%d", &base);
            if (base > 1 && base <= 16) {
                return base;
            } else {
                printf("\nThe base must be between 2 and 16 inclusively.  You typed %d.\n",
                    base);
            }
         }
    }
    

    By the way, even though it isn't standard, there are reasons to use higher bases. A book I have titled Programming as if People Mattered by Nathaniel S. Borenstein (ISBN 0691037639) describes a situation where a college printed out randomly generated IDs on mailing labels. Unfortunately, sometimes the IDs looked like 2378gw48fUCk837 or he294ShiT4823, and recipients thought the school was cursing them.

    A committee convened to suggest words that should not appear in those IDs. A student intern came up with this idea to make the committee unnecessary: Just use base 31 and leave out the vowels. That is, use 0123456789bcdfghjklmnpqrstvwxyz as the base-31 digits. This cut down on the work, made a lot of programming unnecessary, and put people out of a job.

    0 讨论(0)
提交回复
热议问题