Converting an integer to binary in C

后端 未结 12 1078
别跟我提以往
别跟我提以往 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: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 (i0){
                bits[arrayLen--]=a&1;
                a>>=1;
            }
            return bits;
         }
    

提交回复
热议问题