这里有一个
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
typedef unsigned int uint;
typedef unsigned short ushort;
typedef unsigned char uchar;
typedef unsigned long long ullong;
typedef uchar boolean;
#define TRUE 1
#define FALSE 0
char *DecToBin(uint dec){
int i=0;
char *str=NULL;
boolean start=FALSE;
uchar bit_size=0;
boolean b;
for(i=sizeof(uint)*8-1;i>=0;i--){
b=(dec>>i)&1;
if(start){
str[bit_size-i-1]=b?'1':'0';
}else if(b){
start=TRUE;
bit_size=i+1;
printf("bit_size:%u\n",bit_size);
str=(char *)malloc(bit_size+1);
str[bit_size-i-1]=(b)?'1':'0';
}
}
if(!start){
bit_size=1;
str=(char *)malloc(bit_size+1);
str[0]='0';
}
str[bit_size]='\0';
return str;
}
int main()
{
uint i;
char *str;
for(i=0;i<100;i++)
{
str=DecToBin(i);
printf("%u => %s\n",i,str);
free(str);
}
}
只是转换为2进制的