title: Int to Base 2 & 8 Optimization
body: <p>I'm currently reading a C++ book from the very beginning to learn it. I already have coding knowledge but I really want to deeply learn C++ by trying to code as much as possible from my own instead of always use libraries. (While i'm learning. I'll use libraries later.) </p>
  
  <p>I'm currently reading about the casts. While i'm reading, I try to make exercises to apply what I just read. But well this time I lost control and it goes far away of what I was practicing (casts) but it was fun to code.
  [EDIT] I will read <a href="https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used">Different casts</a>. - thanks @Chnossos</p>
  
  <p>I learned a lot while making this code such as stringsstream, reverse etc..</p>
  
  <p>This is a simple int to base 2 and I wish to know if there is a way to optimize the code, or if it's could be correct like this. </p>
  
  <p>Could be great to learn more with an optimized code to see what experts are doing.</p>
  
  <p>Thanks.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
  int toBase_8(int number){ //basé sur un exemple de chiffre 14 avec base 8 -&gt; 016
      int base(8);
      int entier1;
      double entier2;
      double decimal;
      double resTemp;
      int res;
  
      resTemp = (double)number / base; //donne 1.75
  
      entier1 = (int)resTemp; //donne 1 car transforme le double en int alors le int garde seulement l'entier
  
      decimal = resTemp - entier1; //donne 0.75 restant
      entier2 = decimal * (double)base; // donne 6
  
      res = ((entier1*10)+(int)entier2); //donne 16
  
      return res;
  }
  
  int toBase_2(float number){
      if (number &gt; 0 &amp;&amp; number &lt;= 255){ //s'sassure que le nombre est entre 0 et 255
  
          int arr[8];
          int pos(0);
  
          while ((int)number != 0 || pos == 7){ // à chaque tour de boucle, convertie le float en entier et le compare. ajouté le ou pour etre certains de mettre des 0 jusqu'à la fin meme si la premiere condition (!=) est atteinte
              number = number / 2;
  
              if (number == (int)number){
                  arr[pos] = 0; //si entier
              }else{
                  arr[pos] = 1; //si decimal
              }
              number = (int)number; // remet le float en int pour la prochaine division. En base 2 chaque nombre decimal doit etre redivisé en entier. Ex.: (9/2) = 4.5. Donne 1 car decimal. Apres on recommence à partir de (4/2) = 2, et non pas 4.5/2. Donne 0 car entier etc..
              pos++;
          }
          reverse(begin(arr), end((arr))); //reverse le array avec algorithm.h car la réponse binaire ce lit sens inverse.
  
          //mettre le array(int) en string puis en un seul int en utilisant stringstream &lt;sstream&gt;
          stringstream ss;
  
          for (int d(0); d&lt;(sizeof(arr)/ sizeof(arr[0])); d++){
              ss &lt;&lt; arr[d]; //ajoute les int dans le stream
          }
  
          //METHODE 1: utiliser en string
          //cout &lt;&lt; ss.str() &lt;&lt; endl; //sort le stream en string
  
          //METHODE 2: utiliser en int
          int myInt;
          ss &gt;&gt; myInt; //envoie le string dans le int
  
          //cout &lt;&lt; myInt &lt;&lt; endl;
  
          return myInt;
      }else{
  
          return 0;
      }
  }
  
  int main()
  {
      cout &lt;&lt; setw(8) &lt;&lt; setfill('0') &lt;&lt; toBase_2(234) &lt;&lt; endl;
  
      // ex.: 14 en base 8 =&gt; 016
      //cout &lt;&lt; setw(3) &lt;&lt; setfill('0') &lt;&lt; toBase_8(14) &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p><strong>[EDIT - Final]</strong> What I understand from all the answers is in fact that my vision of optimization is not the same as yours. @Chnossos understood the most by gaving me good tips such as cast type differences and dropping all the Double and use Modulo (%) instead. For the rest, I think it's all about your vision of optimization such as use more libraries as possible.</p>
  
  <p>Thanks for the answers. If anybody have other code optimization in the same minding as @Chnossos, your welcome.</p>