Pushok

decimal to binary

Mar 7th, 2011
456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include "iostream"
  2. #include "conio.h"
  3.  
  4. class SomeFunc
  5. {
  6. public:
  7.     static char* tostring(int num)
  8.     {
  9.         char *ch = new char[1024];
  10.         sprintf(ch,"%d",num);
  11.         return ch;
  12.     }
  13.  
  14.     static char* DecToBin(double num)
  15.     {
  16.         if(num == 0) return "0";
  17.         char *bt = new char[1024];
  18.         bt[0] = '\0';
  19.         while(1)
  20.         {
  21.             strcat(bt,tostring(int(num) % 2));
  22.             if(((double)num / 2) == 0.5)
  23.             {
  24.                 for(int i=0,j=strlen(bt)-1;i<floor(double(strlen(bt)/2));i++,j--)
  25.                 {
  26.                     char it = bt[i];
  27.                     bt[i] = bt[j];
  28.                     bt[j] = it;
  29.                 }
  30.                 return bt;
  31.             }
  32.             num = floor(double(num/2));
  33.         }
  34.     }
  35. };
  36.  
  37. void main(int argc,char** argv)
  38. {
  39.     printf("%s = %s",SomeFunc::tostring(512),SomeFunc::DecToBin(512));
  40. }
Advertisement
Add Comment
Please, Sign In to add comment