Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an integer to binary in C

Tags:

c

binary

I'm trying to convert an integer 10 into the binary number 1010.

This code attempts it, but I get a segfault on the strcat():

int int_to_bin(int k)
{
   char *bin;

   bin = (char *)malloc(sizeof(char));
   while(k>0) {
      strcat(bin, k%2);
      k = k/2;
      bin = (char *)realloc(bin, sizeof(char) * (sizeof(bin)+1));
   }
   bin[sizeof(bin)-1] = '\0';

   return atoi(bin);
}

How do I convert an integer to binary in C?

like image 991
JJRhythm Avatar asked Mar 30 '11 15:03

JJRhythm


People also ask

How do you convert int to binary?

To convert integer to binary, start with the integer in question and divide it by 2 keeping notice of the quotient and the remainder. Continue dividing the quotient by 2 until you get a quotient of zero. Then just write out the remainders in the reverse order. Here is an example of such conversion using the integer 12.

Is binary function in C?

Binary literals don't exist in C. The closest you have are hexadecimal, as they follow the binary bitpattern closely.

Is there a function in C that converts decimal to binary?

The programs takes decimal number (entered by user) as input and converts it into a binary number using the function decimalToBinary() . To understand this program, you should have the basic idea of following C programming topics: C – Functions.


3 Answers

If you want to transform a number into another number (not number to string of characters), and you can do with a small range (0 to 1023 for implementations with 32-bit integers), you don't need to add char* to the solution

unsigned int_to_int(unsigned k) {
    if (k == 0) return 0;
    if (k == 1) return 1;                       /* optional */
    return (k % 2) + 10 * int_to_int(k / 2);
}

HalosGhost suggested to compact the code into a single line

unsigned int int_to_int(unsigned int k) {
    return (k == 0 || k == 1 ? k : ((k % 2) + 10 * int_to_int(k / 2)));
}
like image 54
pmg Avatar answered Dec 10 '22 16:12

pmg


You need to initialise bin, e.g.

bin = malloc(1);
bin[0] = '\0';

or use calloc:

bin = calloc(1, 1);

You also have a bug here:

 bin = (char *)realloc(bin, sizeof(char) * (sizeof(bin)+1));

this needs to be:

 bin = (char *)realloc(bin, sizeof(char) * (strlen(bin)+1));

(i.e. use strlen, not sizeof).

And you should increase the size before calling strcat.

And you're not freeing bin, so you have a memory leak.

And you need to convert 0, 1 to '0', '1'.

And you can't strcat a char to a string.

So apart from that, it's close, but the code should probably be more like this (warning, untested !):

int int_to_bin(int k)
{
   char *bin;
   int tmp;

   bin = calloc(1, 1);
   while (k > 0)
   {
      bin = realloc(bin, strlen(bin) + 2);
      bin[strlen(bin) - 1] = (k % 2) + '0';
      bin[strlen(bin)] = '\0';
      k = k / 2;
   }
   tmp = atoi(bin);
   free(bin);
   return tmp;
}
like image 20
Paul R Avatar answered Dec 10 '22 16:12

Paul R


Just use itoa to convert to a string, then use atoi to convert back to decimal.

unsigned int_to_int(unsigned int k) {
    char buffer[65]; /* any number higher than sizeof(unsigned int)*bits_per_byte(8) */
    return atoi( itoa(k, buffer, 2) );
}
like image 37
Andy Finkenstadt Avatar answered Dec 10 '22 18:12

Andy Finkenstadt