Convert decimal to binary in C
Solution 1
The value is not decimal. All values in computer's memory are binary.
What you are trying to do is to convert int to a string using specific base.
There's a function for that, it's called itoa
.
http://www.cplusplus.com/reference/cstdlib/itoa/
Solution 2
First of all 192
cannot be represented in 4
bits
192 = 1100 0000
which required minimum 8
bits.
Here is a simple C program to convert Decimal number system to Binary number system
#include <stdio.h>
#include <string.h>
int main()
{
long decimal, tempDecimal;
char binary[65];
int index = 0;
/*
* Reads decimal number from user
*/
printf("Enter any decimal value : ");
scanf("%ld", &decimal);
/* Copies decimal value to temp variable */
tempDecimal = decimal;
while(tempDecimal!=0)
{
/* Finds decimal%2 and adds to the binary value */
binary[index] = (tempDecimal % 2) + '0';
tempDecimal /= 2;
index++;
}
binary[index] = '\0';
/* Reverse the binary value found */
strrev(binary);
printf("\nDecimal value = %ld\n", decimal);
printf("Binary value of decimal = %s", binary);
return 0;
}
Solution 3
5 digits are not enough for your example (192). Probably you should increase output
Solution 4
A few days ago, I was searching for fast and portable way of doing sprintf("%d", num)
. Found this implementation at the page itoa with GCC:
/**
* C++ version 0.4 char* style "itoa":
* Written by Lukás Chmela
* Released under GPLv3.
*/
char* itoa(int value, char* result, int base) {
// check that the base if valid
if (base < 2 || base > 36) { *result = '\0'; return result; }
char* ptr = result, *ptr1 = result, tmp_char;
int tmp_value;
do {
tmp_value = value;
value /= base;
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)];
} while ( value );
// Apply negative sign
if (tmp_value < 0) *ptr++ = '-';
*ptr-- = '\0';
while(ptr1 < ptr) {
tmp_char = *ptr;
*ptr--= *ptr1;
*ptr1++ = tmp_char;
}
return result;
}
Solution 5
Here is the Algorithm to convert Decimal to Binary number
- Divide the input decimal number by 2 and store the remainder.
- Store the quotient back to the input number variable.
- Repeat this process till quotient becomes zero.
- Equivalent binary number will be the remainders in above process in reverse order.
You can check c program here http://www.techcrashcourse.com/2015/08/c-program-to-convert-decimal-number-binary.html
![mrpopo](https://i.stack.imgur.com/RAIIH.jpg?s=256&g=1)
mrpopo
Updated on June 30, 2021Comments
-
mrpopo almost 3 years
I am trying to convert a decimal to binary such as 192 to 11000000. I just need some simple code to do this but the code I have so far doesn't work:
void dectobin(int value, char* output) { int i; output[5] = '\0'; for (i = 4; i >= 0; --i, value >>= 1) { output[i] = (value & 1) + '0'; } }
Any help would be much appreciated!
-
thetaprime over 11 yearsAnd function sprintf and sscanf
-
Agent_L over 11 years@holmium They don't usually support base-2. On the other hand,
itoa
is also non-standard (but far more common thanprintf("%b"
-
Agent_L over 11 yearswow, nice trick with palindromic array indexed from the middle.
-
Patratacus over 11 yearsWhat's with all the down vote? I did answer the question but not in C. The algorithm is simple enough to implement in C on his own.
-
gjois over 8 years
strrev
is not part of C standard. -
Toby Speight over 6 yearsThank you for this code snippet, which might provide some limited short-term help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made. For instance, how do you address the overflow problem here?
-
Alan over 5 yearsDecimal is another way to say a Base10 number. I believe the author is trying to say how do I convert a Base10 number to a Base2 number (regardless of internal representation).