找了半天,竟然没转为二进制的函数,谢谢了

解决方案 »

  1.   

    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';
    }
      

  2.   

    //只给出正整数的算法
    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++;
      }
    }
      

  3.   

    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);
    }
      

  4.   

    我也写了一个,经调试通过: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';
    }
      

  5.   

    #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;
    }我也写了一个,没什么其他的目的只是为了赚点分 ^_^
      

  6.   

    使用模板:
    #include <bitset>
    #include <string>
    using namespace std;bitset<bits> set(num);//bits为你要转换的二进制位数;num为你想转换的数,如100
    string s=set.to_string();
    //则string中保存的就是100的二进制表示字符串
      

  7.   

    为何各位要这样麻烦,
    试一下如下语句,已经测试过,BUF存入的值为101
    char BUF[10];

    itoa(5,BUF,2);
      

  8.   

    zhucde(【风间苍月】) 的方法最新简单
    收藏
      

  9.   

    用itoa可以转换2,8,16等进制的数字