title: Convert byte array to ushort array in C# .Net Microframework
body: <p>I need to convert a byte array to a UInt16 (ushort) array. I am able to do so from a byte array to a UInt32 array.</p>
  
  <p>I already looked at <a href="https://stackoverflow.com/questions/1104599/convert-byte-array-to-short-array-in-c-sharp">this SO question</a>. But I cannot use BitConverter or the solution given in the referenced question. </p>
  
  <p>I also referred to these questions as well:
  <a href="https://stackoverflow.com/questions/9210373/why-do-shift-operations-always-result-in-a-signed-int-when-operand-is-32-bits">THIS</a> and <a href="https://stackoverflow.com/questions/18798666/cannot-implicity-convert-type-int-to-ushort-already-explicity-cast">THIS</a>.</p>
  
  <p>This is what I have tried so far.</p>
  
  <pre><code>for (uint objIndex = 0; objIndex &lt; data.Length; ++objIndex)
  {
     data[objIndex] = (Convert.ToUInt16(byteArray[objIndex * sizeof(UInt16) + 0].ToString()) &lt;&lt; 8) 
                                 + byteArray[objIndex * sizeof(UInt16) + 1]; 
     // Error - Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists.
  
     data[objIndex] = ((ushort)(byteArray[objIndex * sizeof(UInt16) + 0]) &lt;&lt; 8)
                                 + byteArray[objIndex + 1]; 
     // Error - Cannot implicitly convert type 'int' to 'ushort'. An explicit conversion exists.
  }
  </code></pre>
  
  <p>Please let me know what is missing here.</p>
  
  <p>EDIT:
  I was able to fix it after casting each of the number to an ushort as shown below.</p>
  
  <pre><code>for (ushort objIndex = 0; objIndex &lt; data.Length; ++objIndex)
  {
       ushort length = sizeof(UInt16);
       data[objIndex] = (ushort)( (ushort)(byteArray[objIndex * length] &lt;&lt; (ushort)8) +
                                                   byteArray[objIndex + 1] );
  }
  </code></pre>