Question #6711   Submitted by Answiki on 09/23/2022 at 11:35:17 AM UTC

In C, how do you write a function that converts an integer to binary in a string?

Answer   Submitted by Answiki on 09/23/2022 at 11:49:34 AM UTC

The following C function converts an integer to binary and store the result in a string:

// Convert an integer to binary
// integer : the integer to convert
// binary : the string containing the conversion result
// n : number of bits (8, 16, 32 ...)
void int2bin(unsigned integer, char* binary, int n=8)
{  
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
}

Try it online on repl.it.


There is another, more user-friendly option which returns the string directly. But be careful, the memory needed for the string is allocated in the function, it is necessary to free it outside the function.

// Convert an integer to binary
// integer : the integer to convert
// n : number of bits (8, 16, 32 ...)
// Return a string containing the binary conversion
char* int2bin(unsigned integer, int n=8)
{
  char* binary = (char*)malloc(n+1);
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
  return binary;
}

Try it online on repl.it.


See also:


6 events in history
Answer by Answiki on 09/23/2022 at 11:49:34 AM

The following C function converts an integer to binary and store the result in a string:

// Convert an integer to binary
// integer : the integer to convert
// binary : the string containing the conversion result
// n : number of bits (8, 16, 32 ...)
void int2bin(unsigned integer, char* binary, int n=8)
{  
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
}

Try it online on repl.it.


There is another, more user-friendly option which returns the string directly. But be careful, the memory needed for the string is allocated in the function, it is necessary to free it outside the function.

// Convert an integer to binary
// integer : the integer to convert
// n : number of bits (8, 16, 32 ...)
// Return a string containing the binary conversion
char* int2bin(unsigned integer, int n=8)
{
  char* binary = (char*)malloc(n+1);
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
  return binary;
}

Try it online on repl.it.


See also:


Answer by Answiki on 09/23/2022 at 11:45:53 AM

The following C function converts an integer to binary and store the result in a string:

// Convert an integer to binary
// integer : the integer to convert
// binary : the string containing the conversion result
// n : number of bits (8, 16, 32 ...)
void int2bin(unsigned integer, char* binary, int n=8)
{  
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
}

Try it online on repl.it.


There is another, more user-friendly option which returns the string directly. But be careful, the memory needed for the string is allocated in the function, it is necessary to free it outside the function.

// Convert an integer to binary
// integer : the integer to convert
// n : number of bits (8, 16, 32 ...)
// Return a string containing the binary conversion
char* int2bin(unsigned integer, int n=8)
{
  char* binary = (char*)malloc(n+1);
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
  return binary;
}

Try it online on repl.it.


See also:


Answer by Answiki on 09/23/2022 at 11:40:30 AM

The following C function converts an integer to binary and store the result in a string:

// Convert an integer to binary
// integer : the integer to convert
// binary : the string containing the conversion result
// n : number of bits (8, 16, 32 ...)
void int2bin(unsigned integer, char* binary, int n=8)
{  
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
}

Try it online on repl.it.


There is another, more user-friendly option which returns the string directly. But be careful, the memory needed for the string is allocated in the function, it is necessary to free it outside the function.

// Convert an integer to binary
// integer : the integer to convert
// n : number of bits (8, 16, 32 ...)
// Return a string containing the binary conversion
char* int2bin(unsigned integer, int n=8)
{
  char* binary = (char*)malloc(n+1);
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
  return binary;
}

Try it online on repl.it.


Voire aussi :


Answer by Answiki on 09/23/2022 at 11:39:32 AM

The following C function converts an integer to binary and store the result in a string:

// Convert an integer to binary
// integer : the integer to convert
// binary : the string containing the conversion result
// n : number of bits (8, 16, 32 ...)
void int2bin(unsigned integer, char* binary, int n=8)
{  
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
}

Try it online on repl.it.


There is another, more user-friendly option which returns the string directly. But be careful, the memory needed for the string is allocated in the function, it is necessary to free it outside the function.

// Convert an integer to binary
// integer : the integer to convert
// n : number of bits (8, 16, 32 ...)
// Return a string containing the binary conversion
char* int2bin(unsigned integer, int n=8)
{
  char* binary = (char*)malloc(n+1);
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
  return binary;
}

Try it online on repl.it.


Voire aussi :


Answer by Answiki on 09/23/2022 at 11:39:18 AM

The following C function convert an integer to binary and store the result in a string:

// Convert an integer to binary
// integer : the integer to convert
// binary : the string containing the conversion result
// n : number of bits (8, 16, 32 ...)
void int2bin(unsigned integer, char* binary, int n=8)
{  
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
}

Try it online on repl.it.


There is another, more user-friendly option which returns the string directly. But be careful, the memory needed for the string is allocated in the function, it is necessary to free it outside the function.

// Convert an integer to binary
// integer : the integer to convert
// n : number of bits (8, 16, 32 ...)
// Return a string containing the binary conversion
char* int2bin(unsigned integer, int n=8)
{
  char* binary = (char*)malloc(n+1);
  for (int i=0;i<n;i++)   
    binary[i] = (integer & (int)1<<(n-i-1)) ? '1' : '0';
  binary[n]='\0';
  return binary;
}

Try it online on repl.it.


Voire aussi :


Question by Answiki 09/23/2022 at 11:35:17 AM
In C, how do you write a function that converts an integer to binary in a string?
# ID Query URL Count

Icons proudly provided by Friconix.