Converting an integer to binary in C

后端 未结 12 1210
别跟我提以往
别跟我提以往 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 05:00

    Just use itoa to convert to a string, then use atoi to convert back to decimal.

    unsigned int_to_int(unsigned int k) {
        char buffer[65]; /* any number higher than sizeof(unsigned int)*bits_per_byte(8) */
        return atoi( itoa(k, buffer, 2) );
    }
    

提交回复
热议问题