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!
It looks like this, but be careful, you have to reverse the resulting string :-)
Convert Decimal to Binary in C Language
Here is the Algorithm to convert Decimal to Binary number
You can check c program here http://www.techcrashcourse.com/2015/08/c-program-to-convert-decimal-number-binary.html
//C Program to convert Decimal to binary using Stack
5 digits are not enough for your example (192). Probably you should increase
output
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: