title: Converting 24 bit integer (2s complement) to 32 bit integer in C++
body: <blockquote>
    <p>The dataFile.bin is a binary file with 6-byte records. The first 3
    bytes of each record contain the latitude and the last 3 bytes contain
    the longitude. Each 24 bit value represents radians multiplied by
    0X1FFFFF</p>
  </blockquote>
  
  <p>This is a task I've been working on. I havent done C++ in years so its taking me <em>way</em> longer than I thought it would -_-. After googling around I saw this algorthim which made sense to me. </p>
  
  <pre><code>int interpret24bitAsInt32(byte[] byteArray) {     
   int newInt = (  
       ((0xFF &amp; byteArray[0]) &lt;&lt; 16) |  
       ((0xFF &amp; byteArray[1]) &lt;&lt; 8) |   
       (0xFF &amp; byteArray[2])  
     );  
   if ((newInt &amp; 0x00800000) &gt; 0) {  
     newInt |= 0xFF000000;  
   } else {  
     newInt &amp;= 0x00FFFFFF;  
   }  
  return newInt;  
  }  
  </code></pre>
  
  <p>The problem is a <strong>syntax issue</strong> I am restricting to working by the way the other guy had programmed this. I am not understanding how I can store the CHAR "data" into an INT. Wouldn't it make more sense if "data" was an Array? Since its receiving 24 integers of information stored into a BYTE. </p>
  
  <pre><code>double BinaryFile::from24bitToDouble(char *data) {
      int32_t iValue;
  
      // ****************************
      // Start code implementation
      // Task: Fill iValue with the 24bit integer located at data.
      // The first byte is the LSB.
      // ****************************
  //iValue += 
      // ****************************
      // End code implementation
      // ****************************
      return static_cast&lt;double&gt;(iValue) / FACTOR;
  }
  
  bool BinaryFile::readNext(DataRecord &amp;record)
  {
      const size_t RECORD_SIZE = 6;
      char buffer[RECORD_SIZE];
      m_ifs.read(buffer,RECORD_SIZE);
      if (m_ifs) {
          record.latitude = toDegrees(from24bitToDouble(&amp;buffer[0]));
          record.longitude = toDegrees(from24bitToDouble(&amp;buffer[3]));
          return true;
      }
      return false;
  }
  
  double BinaryFile::toDegrees(double radians) const
  {
      static const double PI = 3.1415926535897932384626433832795;
      return radians * 180.0 / PI;
  }
  </code></pre>
  
  <p>I appreciate any help or hints even if you dont understand a clue or hint will help me alot. I just need to talk to someone. </p>