Thread: Convert int to string in binary base

1. Convert int to string in binary base

Hi
Is there a library function in C which convert int decimal to binary string. I know about itoa function but it is not part of standard.

2. Why don't you try writing your own function to do that? It's pretty straightforward....

There are solutions all over the internet, google em up.

C++ - Decimal to binary converting - Stack Overflow
Program for Decimal to Binary Conversion - GeeksforGeeks
Convert int to string in binary base

3. Originally Posted by gawiellus
Hi
Is there a library function in C which convert int decimal to binary string. I know about itoa function but it is not part of standard.
No, you would need to write that yourself. Not that difficult.

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void deztobin(unsigned int decimalvalue, char zudi[], int prezeros);
void bintodez(char wort[], unsigned int (*dewer) );

int main(int argc, char **argv)
{
int question = 9, bin_str_len = 0;
unsigned int decimalvalue = 0;
char binaerystring[120] = {0};

do
switch(question)
{
default:
printf("\nconverts decimal to a binary string\n");
printf("exit program..................0\n");
printf("decimal to binary.............1\n");
printf("binary to decimal dezimal.....2\n");
printf("set length of binary string...3\n");
scanf("%d", &question);
break;

case 0:
printf("\nEnd of program\n");
return 0;
break;

case 1:
scanf("%d", &decimalvalue);
printf("\ndecimal value is: %d\n", decimalvalue);
deztobin(decimalvalue, binaerystring, bin_str_len);
printf("\nbinary....: %s\n", binaerystring);
printf("____________0123456789123456789012345678901234567890\n");
question = 9;
break;

case 2:
printf("\nPlease enter a binary value up to 32 bit: ");
scanf("%s",binaerystring);
printf("\ndecimal value was: Binaer %s", binaerystring);
bintodez(binaerystring, &decimalvalue);
printf(" decimal %d\n", decimalvalue);
question = 9;
break;

case 3:
printf("\nPlease enter the desired length of the binary string: ");
scanf("%d", &bin_str_len);
if ((bin_str_len < 0) || (bin_str_len > 32))
{
printf("failure: only values from 0 up to 32 allowed\n");
bin_str_len = 0;
}
question = 9;
break;

}while(question != 0);

return 0;
}

/** Version to set the length of the binary string in argument 3
*  if argument 3(prezeros) == 0 No leading zeros are in the string
*  if argument 3(prezeros) is greater than 0
*  fills the string with leading '0'
*  Example:
*  If you set arg 3 to 24
*  decimal: 170
*  binary:  000000000000000010101010  (= 24 characters)
*  If you woud set arg 3 to 3 and
*  decimal would be 31
*  you get '11111' as the binary string
*  or arg 3 = 9
*  decimal: 134
*  binary: 010000110
*
* */

void deztobin(unsigned int decimalvalue, char wort[], int prezeros)
{
char sym = 0;
int slei, sleib, erste = 0;
unsigned int dum2, testvalue, kon = 1;

if ((prezeros < 0) || (prezeros > 32)) prezeros = 0;

sleib = 0;
for (slei = 31; slei >= 0; slei--)
{
testvalue = kon << slei;  /* if only Bit 31 is set (2147483648 decimal) ist auf Char-Nr.0 */
dum2 = decimalvalue & testvalue;
if (dum2)
{
// 'erste' means "first set Bit in value" to make sure that the binary string shows
// all characters when argument 3 is set to 0 or less than numbers of characters where need
if (erste == 0) erste = slei;
sym =  '1';
}
else if (dum2 == 0)
sym =  '0';

wort[sleib] = sym;

if((prezeros > slei) || (erste != 0)) sleib++;
}

}

// converts a binary string with up to 32 characters to a decimal
void bintodez(char wort[], unsigned int (*dewer) )
{
int slei, end_;
unsigned int testvalue = 1, dumza = 0;

end_ = strlen(wort); // if string has 32 characters then  ende == 32

testvalue<<= (end_ - 1); // Anpassung von testvalue to length of string

for (slei = 0; slei < end_; slei++)
if ((wort[slei] == 'I') || (wort[slei] == '1'))
dumza += testvalue >> slei;

(*dewer) = dumza;
}```

5. @rusyoldguy

We are here for advising the OP's on the code that they write, not to code a possible solution for them.