Convert decimal to binary in C
-
I'm attempting to convert a decimal to binary such as 192 to 11000000. I simply need a basic code to do this yet 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';
}
}Prior to this, done thorough research on decimal to binary in cI and gone through some good Articles, to have a clear understanding of the topic. Any help would be much appreciated!
-
I'm attempting to convert a decimal to binary such as 192 to 11000000. I simply need a basic code to do this yet 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';
}
}Prior to this, done thorough research on decimal to binary in cI and gone through some good Articles, to have a clear understanding of the topic. Any help would be much appreciated!
You don’t say exactly why it doesn’t work so I’ll just guess: the code works only with numbers up to 2^5=32. If you tried it with 192 it would have failed. As a general advice, for small algorithms like that, it helps to “play the computer”: take a piece of paper and go through each step as you would be the computer. That gives you a better understanding of how the algorithm works and helps you find eventual bugs.
Mircea
-
I'm attempting to convert a decimal to binary such as 192 to 11000000. I simply need a basic code to do this yet 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';
}
}Prior to this, done thorough research on decimal to binary in cI and gone through some good Articles, to have a clear understanding of the topic. Any help would be much appreciated!
-
I'm attempting to convert a decimal to binary such as 192 to 11000000. I simply need a basic code to do this yet 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';
}
}Prior to this, done thorough research on decimal to binary in cI and gone through some good Articles, to have a clear understanding of the topic. Any help would be much appreciated!
As noted in other answers, your code handles at most four bits. Moreover, your function is assuming a (at least) 5-bytes buffer is provided by the caller. That's a flawn: the caller should provide the size of the buffer and the called should check if the size is big enough to produce the requested output. Try
#include
#include
#includebool uint2binstr(unsigned value, char output[], size_t output_size)
{
unsigned v = value;
unsigned bits = 0;
while ( v )
{
v >>= 1;
++bits;
}
if ( ! bits ) ++bits;if ( output_size > bits )
{
output[bits] = '\0';
while (bits)
{
--bits;
output[bits] = (value & 1) + '0';
value >>= 1;
}
return true;
}
return false;
}enum { N = 33 }; // assuming 'unsigned' is 32 bits
int main()
{
char out[N];unsigned a[] = { 0, 1, 128, 192, 65535, 65536, -1 };
for (size_t n=0; n
"In testa che avete, Signor di Ceprano?"
-- Rigoletto -
As noted in other answers, your code handles at most four bits. Moreover, your function is assuming a (at least) 5-bytes buffer is provided by the caller. That's a flawn: the caller should provide the size of the buffer and the called should check if the size is big enough to produce the requested output. Try
#include
#include
#includebool uint2binstr(unsigned value, char output[], size_t output_size)
{
unsigned v = value;
unsigned bits = 0;
while ( v )
{
v >>= 1;
++bits;
}
if ( ! bits ) ++bits;if ( output_size > bits )
{
output[bits] = '\0';
while (bits)
{
--bits;
output[bits] = (value & 1) + '0';
value >>= 1;
}
return true;
}
return false;
}enum { N = 33 }; // assuming 'unsigned' is 32 bits
int main()
{
char out[N];unsigned a[] = { 0, 1, 128, 192, 65535, 65536, -1 };
for (size_t n=0; n
"In testa che avete, Signor di Ceprano?"
-- RigolettoWell explained. Thank You!
-
Your code is correct as far as the conversion goes. The problem is that it can not handle any binary value greater than 31. You need to calculate the number of digits in the converted value first, and create an array big enough to store the string.
Thanks Richard MacCutchan I will Try this.
-
Well explained. Thank You!
-
I'm attempting to convert a decimal to binary such as 192 to 11000000. I simply need a basic code to do this yet 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';
}
}Prior to this, done thorough research on decimal to binary in cI and gone through some good Articles, to have a clear understanding of the topic. Any help would be much appreciated!