求在C++中把整数转化为2进制的编码

来源:百度知道 编辑:UC知道 时间:2024/05/30 22:27:40
整数不是用cin输入的 是argv[1]..大家懂我的意思吗?
简单可行的就好.

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
int num;
char temp[100], str[100];
memset(temp, 0, sizeof(temp));
memset(str, 0, sizeof(str));
num = atoi(argv[1]);
printf("num = %d, ", num);
while(1)
{
if(1 == num)
{
sprintf(str, "%d%s", num % 2, temp);
memset(temp, 0, sizeof(temp));
strcpy(temp, str);
break;
}
sprintf(str, "%d%s", num % 2, temp);
memset(temp, 0, sizeof(temp));
strcpy(temp, str);
num = num / 2;
}
printf("binary system = %s\n", str);
return 1;
}

//用c写的