2 Replies - 7202 Views - Last Post: 12 September 2010 - 10:09 PM Rate Topic: -----

#1 jaay5389  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 16-December 09

Decimal to Binary Conversion without use of built in library functions

Posted 12 September 2010 - 03:58 PM

The assignment was :
1) Ask user for integer input
2) Convert integer to binary form
Don't use sprintf or sscanf from the Standard C library.

I thought that sprintf and sscanf was different from printf and scanf, so i used those. How would this have been done? I dont understand how printf automatically converts the integer to binary to back to integer to display on the screen. Here was my code.

#include <stdio.h>
#include <math.h>
#include <string.h>
int x,y,result1,z,i;
int t[9], final[9];
int c;
int main()
{
printf("\n");
/* Display Prompt 3 */
printf("Enter an integer from 0 to 255: ");
/* Get Input 2 */
	scanf("%d",&x);
	printf("you entered: %d\n",x);
/* Convert Decimal integer to Binary */
result1 = x % 2;
if (x>=0 && x<=255)
	{
		while (x>0)
		{
		z= x % 2;
		x=x/2;
		t[i]=z;
		i++;
		}
printf("The binary equivalent is = ");
printf("%d", t[7]);
printf("%d", t[6]);
printf("%d", t[5]);
printf("%d", t[4]);
printf("%d", t[3]);
printf("%d", t[2]);
printf("%d", t[1]);
printf("%d", t[0]);
printf("\n");
	}
else
	{
	printf("Sorry, you have entered an invalid integer\n");
	return 1;
	}
/* Display Result 4 */
}


Thank you for your input!

Is This A Good Question/Topic? 0
  • +

Replies To: Decimal to Binary Conversion without use of built in library functions

#2 Aphex19  Icon User is offline

  • Born again Pastafarian.
  • member icon

Reputation: 615
  • View blog
  • Posts: 1,873
  • Joined: 02-August 09

Re: Decimal to Binary Conversion without use of built in library functions

Posted 12 September 2010 - 04:46 PM

As far as I know, sprintf does not support converting decimal to binary, although, it does support hexadecimal. The only function i know of that literally converts between bases is strtoul
Was This Post Helpful? 0
  • +
  • -

#3 buffalobill  Icon User is offline

  • D.I.C Head

Reputation: 21
  • View blog
  • Posts: 188
  • Joined: 08-July 08

Re: Decimal to Binary Conversion without use of built in library functions

Posted 12 September 2010 - 10:09 PM

What happens when you compile and run your code? It does not appear to make the change from decimal to binary format.
Since there is no manipulator for converting from decimal to binary format as there is for decimal to hex for example (as far as I know) you need something like a function to perform the conversion.
If n= the user provided integer.
1. find what max power of 2 < n and assign this to (say)k
2. now within a while loop with condition k>0
3. find value of (n/k)%2
4. if (int(n/k)%2==0) write 0 else write 1.
5. divide k by 2
6. loop to 3 above and repeat
If n=255 this function would print 11111111.

This post has been edited by buffalobill: 13 September 2010 - 05:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1