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!
You can also use the 'if', 'else', statements to write this code.
First of all
192
cannot be represented in4
bits192 = 1100 0000
which required minimum8
bits.Here is a simple C program to convert Decimal number system to Binary number system