Can some one please recommend me a link or the code for converting binary to decimal, hexadecimal and octal. Another for converting octal to binary, decimal and hexadecimal. Thank you and please help. I really do need it. Again, thank you and have a nice day! :)

sprintf() will do all the conversions except binary. Google for binary to decimal convertions and you will find out how to do that too. If you are not allowed to use sprintf(), such as you must write the algorithms yourself, then use google because such algorithms are very common and have been posted for quite a few years now.

Edited 6 Years Ago by Ancient Dragon: n/a

How do I filter the user's input to only binary and octal numbers? Here's the code:

#include <stdio.h>
#include <stdlib.h>

main ()
{
  int i,n,a;
  char buffer [33];
while (a>5)
{
printf ("Enter:\n         [1]\n to convert binary to\n\n   decimal;\n   hexadecimal; and\n   octal;\n\n         [2]\n to convert octal to\n\n   decimal;\n   hexadecimal;and\n   binary.\n");

scanf("%d", &n);

if(n==1)
{

printf("Enter a Binary number.\n");
scanf ("%d",&i);
  itoa (i,buffer,10);
  printf ("\ndecimal: %s\n",buffer);
  itoa (i,buffer,16);
  printf ("hexadecimal: %s\n",buffer);
  itoa (i,buffer,8);
  printf ("octal: %s\n",buffer);
    a=4;    
}


else if(n==2)
{
printf("Enter an octal number.\n");
  scanf ("%d",&i);
  itoa (i,buffer,10);
  printf ("decimal: %s\n",buffer);
  itoa (i,buffer,16);
  printf ("hexadecimal: %s\n",buffer);
  itoa (i,buffer,2);
  printf ("binary: %s\n",buffer);   
    a=4;
}



else
{
printf("Sorry, the number you entered is neither 1 nor 2.\n\n");

}

}

getchar();
return 0;
}

The user is able to enter any number even if he only chose conversion of binary, so how can I solve that?... Thank you and please do reply.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

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