# Decimal to Binary Conversion without use of built in library functions

Page 1 of 1

## 2 Replies - 13353 Views - Last Post: 12 September 2010 - 10:09 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=190107&amp;s=7bec4113a1fcd2c474d5d00a51defa1d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jaay5389

Reputation: 0
• 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 */
}
```

Is This A Good Question/Topic? 0

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

### #2 Aphex19

• Born again Pastafarian.

Reputation: 619
• 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

### #3 buffalobill

Reputation: 21
• 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