Converting an integer to binary in C

后端 未结 12 1076
别跟我提以往
别跟我提以往 2021-02-02 04:30

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)
{
          


        
相关标签:
12条回答
  • 2021-02-02 04:36

    The working solution for Integer number to binary conversion is below.

    int main()
    {
        int num=241; //Assuming 16 bit integer
        for(int i=15; i>=0; i--) cout<<((num >> i) & 1);
        cout<<endl;
        for(int i=0; i<16; i++) cout<<((num >> i) & 1);
        cout<<endl;
        return 0;
    }
    

    You can capture the cout<< part based on your own requirement.

    0 讨论(0)
  • 2021-02-02 04:37

    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)));
    }
    
    0 讨论(0)
  • 2021-02-02 04:38

    You could use this function to get array of bits from integer.

        int* num_to_bit(int a, int *len){
            int arrayLen=0,i=1;
            while (i<a){
                arrayLen++;
                i*=2;
            }
            *len=arrayLen;
            int *bits;
            bits=(int*)malloc(arrayLen*sizeof(int));
            arrayLen--;
            while(a>0){
                bits[arrayLen--]=a&1;
                a>>=1;
            }
            return bits;
         }
    
    0 讨论(0)
  • 2021-02-02 04:39

    You can convert decimal to bin, hexa to decimal, hexa to bin, vice-versa etc by following this example. CONVERTING DECIMAL TO BIN

    int convert_to_bin(int number){
        int binary = 0, counter = 0;
        while(number > 0){
            int remainder = number % 2;
            number /= 2;
            binary += pow(10, counter) * remainder;
            counter++;
        }   
    }
    

    Then you can print binary equivalent like this:

    printf("08%d", convert_to_bin(13)); //shows leading zeros

    0 讨论(0)
  • 2021-02-02 04:39

    Result in string

    The following function converts an integer to binary in a string (n is the number of bits):

    // Convert an integer to binary (in a string)
    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';
    }
    

    Test online on repl.it.

    Source : AnsWiki.

    Result in string with memory allocation

    The following function converts an integer to binary in a string and allocate memory for the string (n is the number of bits):

    // Convert an integer to binary (in a string)
    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;
    }
    

    This option allows you to write something like printf ("%s", int2bin(78)); but be careful, memory allocated for the string must be free later.

    Test online on repl.it.

    Source : AnsWiki.

    Result in unsigned int

    The following function converts an integer to binary in another integer (8 bits maximum):

    // Convert an integer to binary (in an unsigned)
    unsigned int int_to_int(unsigned int k) {
        return (k == 0 || k == 1 ? k : ((k % 2) + 10 * int_to_int(k / 2)));
    }
    

    Test online on repl.it

    Display result

    The following function displays the binary conversion

    // Convert an integer to binary and display the result
    void int2bin(unsigned integer, int n=8)
    {  
      for (int i=0;i<n;i++)   
        putchar ( (integer & (int)1<<(n-i-1)) ? '1' : '0' );
    }
    

    Test online on repl.it.

    Source : AnsWiki.

    0 讨论(0)
  • 2021-02-02 04:40
    void intToBin(int digit) {
        int b;
        int k = 0;
        char *bits;
    
        bits= (char *) malloc(sizeof(char));
        printf("intToBin\n");
        while (digit) {
            b = digit % 2;
            digit = digit / 2;
            bits[k] = b;
            k++;
    
            printf("%d", b);
        }
        printf("\n");
        for (int i = k - 1; i >= 0; i--) {
            printf("%d", bits[i]);
    
        }
    
    }
    
    0 讨论(0)
提交回复
热议问题