#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUF_SIZE (100U)
int i_to_b(int a, char *str)
{
int i = 0, j = 0;
char tmp[BUF_SIZE];
if (a == 0) {
str[0] = '0';
return 0;
}
while (a != 0) {
tmp[i] = (a & 0x1) + '0';
i++;
a = a >> 1;
}
for (j = 0; j < i; j++) {
str[j] = tmp[i - j - 1];
}
return 0;
}
int main(int argc, char ** argv)
{
int a = 7;
char str[BUF_SIZE] = {
0 };
i_to_b(a, str);
fprintf(stdout, "%s\n", str);
return 0;
}
纸上验算