请问怎么把十进制转为二进制?如5变为本101 找了半天,竟然没转为二进制的函数,谢谢了 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Copy :void dec_to_binstr(char *num,char *str){ int count1,count2,count3,a; char *p; double x[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, }; count1 = count2 = count3 = 0; for (a = 0; a <= 15; a++) if (*num >= x[a]) { count1 = a; count3 = a; } while (count2 <= count1) { p = &str[count2]; for (a = 0; a <= 1; a ++) if (*num >= a*x[count3] && *num < (a+1)*x[count3]) { *p = a + '0'; *num = *num - a*x[count3]; } count3--; count2++; } p = &str[count2]; *p = '\0';} //只给出正整数的算法void itob(int i, char *buf){ int left, j; char ch; char *p=buf; while (i > 0) { left = i % 2; i = i >> 1; ch = '0' + left; *p++ = ch; } *p='\0'; char *q = buf + strlen(buf) - 1; p = buf; while (p < q) { ch = *p; *p = *q; *q = ch; p++; q++; }} void dec_to_binstr(char *num,char *str){int nn=atoi(num);CString cs="";while(nn>0){ if(nn-nn/2*2==1) cs.Insert(0,"1"); else cs.Insert(0,"0"); nn=nn>>1;}lstrcpy(str,cs);} 我也写了一个,经调试通过:void dec_to_binstr(int num,char *str){ if(num==0){*str++='0';*str='\0';return;} while(!(num&0x80000000))num<<=1; while(num) { if(num&0x80000000) *str++='1'; else *str++='0'; num<<=1; } *str='\0';} #include <iostream.h>void ToBinary( int A ){ static int counter = 0; int B; B = A & 0x00000001; A >>= 1; counter++; if( counter != 32 ) ToBinary( A ); cout<<B; }main(){ int A = 10; ToBinary( A ); return 0;}我也写了一个,没什么其他的目的只是为了赚点分 ^_^ 使用模板:#include <bitset>#include <string>using namespace std;bitset<bits> set(num);//bits为你要转换的二进制位数;num为你想转换的数,如100string s=set.to_string();//则string中保存的就是100的二进制表示字符串 为何各位要这样麻烦,试一下如下语句,已经测试过,BUF存入的值为101char BUF[10]; itoa(5,BUF,2); zhucde(【风间苍月】) 的方法最新简单收藏 用itoa可以转换2,8,16等进制的数字 怎样用键盘控制五子棋下棋(mfc的代码) 问个绝对路径互相包含的算法问题 大家都是用d版vc来开发所谓的"正版"软件?? 关于SDK和Windows消息传递的问题 一个从网站安装程序的问题 请教ADO在SQL Server中随机抽取N条记录的方法 请大家推荐一本vc7.0的经典书籍,类似VC4.0技术内幕的,小弟万分感谢!! 数据库问题请大家帮忙 急!为何不能写入?? 如何求得LPVOID所指内容的SIZE(字节)? 模拟键盘消息alt+s, 如何做? CListCtrl的2 个问题 很急的阿
void dec_to_binstr(char *num,char *str)
{
int count1,count2,count3,a;
char *p;
double x[] =
{
1,
2,
4,
8,
16,
32,
64,
128,
256,
512,
1024,
2048,
4096,
8192,
16384,
32768,
};
count1 = count2 = count3 = 0;
for (a = 0; a <= 15; a++)
if (*num >= x[a])
{
count1 = a;
count3 = a;
}
while (count2 <= count1)
{
p = &str[count2];
for (a = 0; a <= 1; a ++)
if (*num >= a*x[count3] && *num < (a+1)*x[count3])
{
*p = a + '0';
*num = *num - a*x[count3];
}
count3--;
count2++;
}
p = &str[count2];
*p = '\0';
}
void itob(int i, char *buf)
{
int left, j;
char ch;
char *p=buf;
while (i > 0)
{
left = i % 2;
i = i >> 1;
ch = '0' + left;
*p++ = ch;
}
*p='\0';
char *q = buf + strlen(buf) - 1;
p = buf;
while (p < q)
{
ch = *p;
*p = *q;
*q = ch;
p++;
q++;
}
}
{
int nn=atoi(num);CString cs="";
while(nn>0)
{
if(nn-nn/2*2==1)
cs.Insert(0,"1");
else cs.Insert(0,"0");
nn=nn>>1;
}
lstrcpy(str,cs);
}
{
if(num==0){*str++='0';*str='\0';return;}
while(!(num&0x80000000))num<<=1;
while(num)
{
if(num&0x80000000)
*str++='1';
else
*str++='0';
num<<=1;
}
*str='\0';
}
void ToBinary( int A )
{
static int counter = 0;
int B; B = A & 0x00000001;
A >>= 1; counter++; if( counter != 32 )
ToBinary( A ); cout<<B;
}main()
{
int A = 10;
ToBinary( A ); return 0;
}我也写了一个,没什么其他的目的只是为了赚点分 ^_^
#include <bitset>
#include <string>
using namespace std;bitset<bits> set(num);//bits为你要转换的二进制位数;num为你想转换的数,如100
string s=set.to_string();
//则string中保存的就是100的二进制表示字符串
试一下如下语句,已经测试过,BUF存入的值为101
char BUF[10];
itoa(5,BUF,2);
收藏