title: 2's complement converter in C++
body: <p>I'm getting stuck and I've been looking at my code for hours and I can't seem to figure out whats wrong. What my function does is that it will take in either a negative or positive number and convert it into its 32-bit 2’s complement hexadecimal representations. My function takes in an array of characters (in 32 bit signed magnitude representation) and take the 2's complement of the array. My approach to converting it to 2's complement is simply assign a character into the array when a certain character is seen. Special case occurs if the character seen in the array is the first one. The rest is normally negated. I'm currently stuck. When I enter the value -23 (signed magnitude is 80000017) I get the value K00000KK. -23452 (signed magnitude 80005B9C) --> K000KKKK. I set the value to be 'K' to find out where the code was getting stuck at, the original value was '7'. In the code, I put display messages to see where the compiler is going</p>
  
  <pre><code>for -23 (80000017) input I get the messages
  
  first 1 is seen, leave first 1 the same
  first 1 is seen, leave first 1 the same
  no one's showed up yet --&gt; 0
  no one's showed up yet --&gt; 0
  no one's showed up yet --&gt; 0
  no one's showed up yet --&gt; 0
  no one's showed up yet --&gt; 0
  first 1 is seen, leave first 1 the same
  </code></pre>
  
  <p>Pretty much where K is shown is where i get the first 1 message and where 0 is shown i get the --> 0 message</p>
  
  <p>Thank you for your help! much appreciated! </p>
  
  <p>Here is a pastebin link to my code because the formatting of the code annoyed me...</p>
  
  <p><a href="http://pastebin.com/KSeymwcs" rel="nofollow">http://pastebin.com/KSeymwcs</a></p>
  
  <pre><code>char* two_complement(int number, char* sign_mag_array){
  
  int first_one_flag = 0;
  
  if(number &gt; 0){
      cout &lt;&lt; "positive number" &lt;&lt; endl;
      return sign_mag_array;
  }
  
  if(number &lt; 0){
      for(int q = 7; q &gt;= 0; q--){
          if(first_one_flag == 1){                // negate the rest regularly
              cout &lt;&lt; "negate normally " &lt;&lt; endl;
  
              if(sign_mag_array[q] == '0'){
                  sign_mag_array[q] = 'F';
              }
              if(sign_mag_array[q] == '1'){
                  sign_mag_array[q] = 'E';
              }
              if(sign_mag_array[q] == '2'){
                  sign_mag_array[q] = 'D';
              }
              if(sign_mag_array[q] == '3'){
                  sign_mag_array[q] = 'C';
              }
              if(sign_mag_array[q] == '4'){
                  sign_mag_array[q] = 'D';
              }   
              if(sign_mag_array[q] == '5'){
                  sign_mag_array[q] = 'A';
              }
              if(sign_mag_array[q] = '6'){
                  sign_mag_array[q] = '9';
              }
              if(sign_mag_array[q] == '7'){
                  sign_mag_array[q] = '8';
              }
              if(sign_mag_array[q] == '8'){
                  sign_mag_array[q] = '7';
              }
              if(sign_mag_array[q] = '9'){
                  sign_mag_array[q] = '6';
              }   
              if(sign_mag_array[q] == 'A'){
                  sign_mag_array[q] = '5';
              }   
              if(sign_mag_array[q] == 'B'){
                  sign_mag_array[q] = '4';
              }
              if(sign_mag_array[q] = 'C'){
                  sign_mag_array[q] = '3';
              }
              if(sign_mag_array[q] == 'D'){
                  sign_mag_array[q] = '2';
              }
              if(sign_mag_array[q] == 'E'){
                  sign_mag_array[q] = '1';
              }
              if(sign_mag_array[q] == 'F'){
                  sign_mag_array[q] = '0';
              }
          }
  
  
          if(sign_mag_array[q] == '0' &amp;&amp; first_one_flag == 0){
              cout &lt;&lt; "no one's showed up yet --&gt; 0 " &lt;&lt; endl;
          }
          else{                                               // first '1' in binary seen. special negating
              //first_one_flag = 1;               
              cout &lt;&lt; "first 1 is seen, leave first 1 the same " &lt;&lt; endl;
  
              if(sign_mag_array[q] == '1'){
                  sign_mag_array[q] = 'F';        
              }
              if(sign_mag_array[q] == '2'){
                  sign_mag_array[q] = 'E';
              }
              if(sign_mag_array[q] == '3'){
                  sign_mag_array[q] = 'D';
              }
              if(sign_mag_array[q] == '4'){
                  sign_mag_array[q] = 'C';
              }
              if(sign_mag_array[q] == '5'){
                  sign_mag_array[q] = 'B';
              }
              if(sign_mag_array[q] == '6'){
                  sign_mag_array[q] = 'A';
              }
              if(sign_mag_array[q] = '7'){
                  sign_mag_array[q] = '9';
              }
              if(sign_mag_array[q] == '8'){
                  sign_mag_array[q] = '8';
              }
              if(sign_mag_array[q] == '9'){
                  sign_mag_array[q] = 'K';
              }       
              if(sign_mag_array[q] == 'A'){
                  sign_mag_array[q] = '6';
              }   
              if(sign_mag_array[q] == 'B'){
                  sign_mag_array[q] = '5';
              }
              if(sign_mag_array[q] == 'C'){
                  sign_mag_array[q] = '4';
              }
              if(sign_mag_array[q] == 'D'){
                  sign_mag_array[q] = '3';
              }
              if(sign_mag_array[q] == 'E'){
                  sign_mag_array[q] = '2';
              }
              if(sign_mag_array[q] == 'F'){
                  sign_mag_array[q] = '1';
              }
          }
      }    
  }
  return sign_mag_array;
  </code></pre>
  
  <p>}</p>