Thread: Convert int to string in binary base

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    9

    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. #2
    Registered User
    Join Date
    Aug 2019
    Location
    Inside a Singularity
    Posts
    169
    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. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    677
    Quote Originally Posted by gawiellus View Post
    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.

  4. #4
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    31
    May be here is an example what can help you:
    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");
          printf("Your choice: ");
          scanf("%d", &question);
        break;
    
      
      case 0:
          printf("\nEnd of program\n");
          return 0;   
        break;  
    
      case 1:
          printf("\nPlease enter a decimal: ");
          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);
          printf("Your choice was: %d\n", 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. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    677
    @rusyoldguy

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you convert a string to MIME base 64?
    By HelpfulPerson in forum C Programming
    Replies: 9
    Last Post: 12-01-2013, 02:22 PM
  2. Convert binary data to string
    By jean.yves in forum C Programming
    Replies: 16
    Last Post: 08-26-2010, 04:09 AM
  3. Convert binary to decimal string
    By Devils Child in forum C# Programming
    Replies: 9
    Last Post: 01-26-2010, 07:37 AM
  4. convert string of binary to Char
    By yagmai in forum C Programming
    Replies: 2
    Last Post: 03-10-2008, 12:37 PM
  5. Replies: 9
    Last Post: 10-07-2006, 05:37 AM

Tags for this Thread