Hi everyone my Binary2Decimal is okay now because it can now hold 16 digits. But how come my Octal2Decimal and Hexa2Decimal can't hold 16 digits to convert to decimal it can only up to 5 digits. Can anyone correct this problem?

#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>

char base;
char key;

void Binary2Decimal();
void Octal2Decimal();
void Hexa2Decimal();

void main()
     {
       do{
       clrscr();
       gotoxy(1,3);printf("Conversion from any base to base 10");
       gotoxy(1,5);printf("a - Binary");
       gotoxy(1,6);printf("b - Octal");
       gotoxy(1,7);printf("c - Hexadecimal");
       gotoxy(1,11);printf("Select a base to be converted: ");
       scanf("%c", &base);

       if((base=='a')||(base=='A')){
       Binary2Decimal();  }
       if((base=='b')||(base=='B')){
       Octal2Decimal();   }
       if((base=='c')||(base=='C')){
       Hexa2Decimal();    }
       if((base!='a')&&(base!='A')&&(base!='b')&&(base!='B')&&(base!='c')
       &&(base!='C')){gotoxy(32,11);printf("Wrong letter"); }

       gotoxy(1,20);printf("Press <ENTER> to Continue or Press <ANY KEY> to Exit");
       scanf("%c", &key);
       key = getch();
       }
       while(key == 13);
       }

void Binary2Decimal()
       {
       char buffer[17];
       int len,i,total=0,value;
       gotoxy(1,13);printf("[BINARY TO DECIMAL CONVERSION]");
       gotoxy(1,15);printf("Enter a Binary number: ");  //accept a maximum of 16 chars
       scanf("%16s", buffer);
       printf("\n");

       len=strlen(buffer);
       for(i=0;i<len;++i)
       {
       value=buffer[i]-'0';
       if (value!=0 && value!=1)
       {
       printf("Invalid binary data\n");
       return;
       }
       total=(total<<1|value);
       }
       printf("Decimal equivalent is: %d\n", total);
       }

void Octal2Decimal()
       {
       char oct8[17];
       unsigned long dec8,i8,sum8,len8;

       sum8=0;
       gotoxy(1,13);printf("[OCTAL TO DECIMAL CONVERSION]");
       gotoxy(1,15);printf("Enter an Octal number: ");
       scanf("%16s", oct8);
       printf("\n");

       len8=strlen(oct8);
       for(i8=0;i8<len8;++i8)
       {
       dec8=oct8[i8]-'0';
       sum8=(sum8*8)+dec8;

       }
       printf("Decimal equivalent is: %u", sum8);
       }

void Hexa2Decimal()
       {
       char hex16[17];
       unsigned long dec16,i16,sum16,len16;

       sum16=0;
       dec16=0;

       gotoxy(1,13);printf("[HEXADECIMAL TO DECIMAL CONVERSION]");
       gotoxy(1,15);printf("Enter a Hexadecimal number: ");
       scanf("%16s", hex16);
       printf("\n");

       len16=strlen(hex16);
       for(i16=0;i16<len16;++i16)
       {
       if(isdigit(hex16[i16]))
       {
       dec16=hex16[i16]-'0';
       }
       else
       {
       dec16=toupper(hex16[i16])-'A'+10;
       }
       sum16=(sum16*16)+dec16;
       }
       printf("Decimal equivalent is: %u", sum16);
       }

Edited 2 Years Ago by john.kane.100483

Because each digit in an octal value is equal to three bits of binary, and each hex digit is equal to four bits. The higher the base, the more compact the representation. This is related to why decimal doesn't suit many purposes in computer mathematics - base 10 doesn't line up to an exact number of bits, that is to say, three bits is too small to hold a decimal number (it can only represent eight values), while 4 bits is too large (it can represent up to sixteen values, not just ten).

If you look at this table:

BASE     Dig Max Decimal String rep (note string len = Dig col value)
=======  ==  ==========  ================================
base  2, 32, 4294967295, 11111111111111111111111111111111
base  3, 21, 4294967295, 102002022201221111210
base  4, 16, 4294967295, 3333333333333333
base  5, 14, 4294967295, 32244002423140
base  6, 13, 4294967295, 1550104015503
base  7, 12, 4294967295, 211301422353
base  8, 11, 4294967295, 37777777777
base  9, 11, 4294967295, 12068657453
base 10, 10, 4294967295, 4294967295
base 11, 10, 4294967295, 1904440553
base 12,  9, 4294967295, 9BA461593
base 13,  9, 4294967295, 535A79888
base 14,  9, 4294967295, 2CA5B7463
base 15,  9, 4294967295, 1A20DCD80
base 16,  8, 4294967295, FFFFFFFF
base 17,  8, 4294967295, A7FFDA90
base 18,  8, 4294967295, 704HE7G3
base 19,  8, 4294967295, 4F5AFF65
base 20,  8, 4294967295, 3723AI4F
base 21,  8, 4294967295, 281D55I3
base 22,  8, 4294967295, 1FJ8B183
base 23,  8, 4294967295, 1606K7IB
base 24,  7, 4294967295, MB994AF
base 25,  7, 4294967295, HEK2MGK
base 26,  7, 4294967295, DNCHBNL
base 27,  7, 4294967295, B28JPDL
base 28,  7, 4294967295, 8PFGIH3
base 29,  7, 4294967295, 76BEIGF
base 30,  7, 4294967295, 5QMCPQF
base 31,  7, 4294967295, 4Q0JTO3
base 32,  7, 4294967295, 3VVVVVV
base 33,  7, 4294967295, 3AOKQ93
base 34,  7, 4294967295, 2QHXJLH
base 35,  7, 4294967295, 2BR45QA
base 36,  7, 4294967295, 1Z141Z3
Enter the base for the number to be converted:

Click Here

You can see the max string rep for each base, 2..36, if the the decimal max storage size is a 32 bit unsigned int

Edited 2 Years Ago by David W: added line

This article has been dead for over six months. Start a new discussion instead.