欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C-十进制转二进制

程序员文章站 2022-07-15 09:39:56
...

include <stdlib.h>

#include <stdio.h>
#include<string.h>
char * int_to_binary(int n)
{
    static char str[100] = "";
    int temp;
    temp = n % 2;
    n = n >> 1;
    
    if (n != 0 ){
        int_to_binary(n);
    }
    char num_str[2];//字符串
    sprintf(num_str, "%d", temp);//数字转字符串
    strcat(str,num_str); //连接两个字符串,连接后的字符串存放在num_str中,数组num_str中有足够空间
    
    return str;
}