Pushok icon

decimal to binary

Pushok | PRO | 03/07/11 07:08:28 PM UTC | 0 ⭐ | 445 👁️ | Never ⏰ | []
C++ |

700 B

|

None

|

0 👍

/

0 👎

#include "iostream"
#include "conio.h"
 
class SomeFunc
{
public:
    static char* tostring(int num)
    {
        char *ch = new char[1024];
        sprintf(ch,"%d",num);
        return ch;
    }
 
    static char* DecToBin(double num)
    {
        if(num == 0) return "0";
        char *bt = new char[1024];
        bt[0] = '\0';
        while(1)
        {
            strcat(bt,tostring(int(num) % 2));
            if(((double)num / 2) == 0.5)
            {
                for(int i=0,j=strlen(bt)-1;i<floor(double(strlen(bt)/2));i++,j--)
                {
                    char it = bt[i];
                    bt[i] = bt[j];
                    bt[j] = it;
                }
                return bt;
            }
            num = floor(double(num/2));
        }
    }
};
 
void main(int argc,char** argv)
{
    printf("%s = %s",SomeFunc::tostring(512),SomeFunc::DecToBin(512));
}

Comments