Here is what i coded for a simple binary to octal converion. If no of digits in binary are multiple of 3 then it works fine but get crashed otherwise . . . . am not able to figure our how to pad up for the grouping of 3 when no of binary didgits are not multiple of 3.

Here is my code.

Please avoid validate(). It works fine.
Other solutions are most welcomed.

// Program for converting a binary into octal equivalent.

#include<stdio.h>
#include<conio.h>
#define MAX 1000

int validate(char *);

int main()
{
    printf("\t* * * * * PROGRAM TO CONVERT BINARY INTO OCTAL NUMBER * * * * *");
    char bin[MAX],oct[MAX];
    int sum=0,re,i=0,o=0,k=1,j=1;
    start:
    printf("\n\nEnter a binary number: ");
    gets(bin);
    re=validate(bin);
    if (re==1)
    {
       goto start;
    }
    else if(re==0)
    {
        goto end;
    }
    else if(re==2)
    {
         // ------- Conversion Begin.
         while(bin[i]!='\0')
         {
             if(k<=3)
             {
                 sum=sum+((((int)bin[i])-48)*j);
                 j=j*2;
                 k++;       
                 i++;
                 if (k==4)
                 {
                     oct[o]=sum;
                     o++;
                     j=1;
                     sum=0;
                     k=1;         
                 }    
             }
         }
         // ------- Conversion end ^^.
          printf("\n\nThe octal equivalent is: ");
         for(i=o-1; i>=0; i--)
         {
          printf("%d", oct[i]);
         }
    }
    getch();
    end:
    return 0;
}

//---- Function for validation.
int validate(char bin[])
{
     int i=0;
     char ch;
     while(bin[i])
    {
       if (bin[i]=='1' || bin[i]=='0')
       {
          // Do Nothing.
       }
       else
       {
           puts("\nYou entered an invalid binary number. \tDo you want to continue (y/n) ?");
           ch=getchar();
           getchar();
           if (ch=='y' || ch=='Y')
           {
              return 1;
           }
           else
           {
               return 0;
           }   
       }
       i++;
    }
    return 2;
}

Thanx.

Edited 3 Years Ago by deceptikon: Seriously, dude. You've been around long enough to know where such questions go.

So Now I have 2 infraction points. I don't see which rule i broke.
This is my original code, i havent copied it from any website. Neither i found any reply to this specific question in daniweb.com, hence its not even repetition.

Thanx

If you want to print the number in octal format, then in your printf() statement, use %o for the format specifier. IE,

/* Instead of this */
printf("%d", oct[i]);
/* Do this */
printf("%o", oct[i]);

use %o for the format specifier.

Unfortunately am trying to do it without using any kind of specifiers . . .neither %o for octal or %x for hex.

So Now I have 2 infraction points. I don't see which rule i broke.

You've been here long enough to know we have a C forum and that C questions go in the C forum. I had to move this thread from Community Center, and thus felt justified in giving you an infraction for Keep It Organized.

If you read the PM that was sent automatically with my reason comment, you'd know this though.

Edited 3 Years Ago by deceptikon

Ok. Try this:

printf("%d", oct[i] & 0x07);

If you need more than one character printed, then you will have to print/shift/print...

Edited 3 Years Ago by rubberman

Ok. Try this:

I need to design an algorithm from scratch rather than using inbuilt functionality.

1st of all , your goto's make it not only uncomfortable to read the code , but also , if you get into a habit of using them , i dont think thats gonna be a good idea.

2nd , give this a read.

1st of all , your goto's make it not only uncomfortable to read the code

Just used it once for the chocie of restarting program.

2nd , give this a read.

Am aware of mathematical algorithm for binary to octal conversion, that you have to group binary digits into 3 from right and subsitute respective octal digit.

Problem here is implementing padding.

Am aware of mathematical algorithm for binary to octal conversion, that you have to group binary digits into 3 from right and subsitute respective octal digit.

so your thinking of a lookup table. but i dont see any such code in what you have posted , also this is a shortcut in some sense . An actual algorithm is a generic process that always works , lookup tables are specific to the two bases that are being consedered.

the actual "algorithm" is this :

any base to dec :   sum: ith-num*(base^i) 
dec to any base :   succ div by required base

default way to convert from base A to base B :
convert A to dec , take that dec and convert to B.
this will work with all bases.

ps :
i post this generic method because you mentioned about "an algorithm from scratch". This might not be the most efficientway to convert between two bases, but thats a different topic.

Edited 3 Years Ago by somjit{}

Yea you are right. Lookup table is a good idea but it is lame and so do this ---> (convert A to dec , take that dec and convert to B.), Coz i have tried this and it works swiftly.

Looking for some efficient solution: like from binary to octal without any intermediate decimal.

Coz i have tried this and it works swiftly.

so something that you have tried and works swiftly is lame ? ;)

lame would be something that is slow , takes a lot of memory etc etc etc.

lookup table may not necessarily be the fastest way to do things. what is fast and what is lame needs actual benchmarking. and a thorough understanding of exactly what it is that your doing.

so something that you have tried and works swiftly is lame ? ;)

Nope Dude, don't get me wrong, but that approach is too mainstream.

atleast you agree that lookup table is not fast.
and bin --> dec --> oct is too mainstream.

This question has already been answered. Start a new discussion instead.