title: Converting a byte array to an int array in C
body: <p>I have some code below that is supposed to be converting a <a href="http://en.wikipedia.org/wiki/C_%28programming_language%29" rel="nofollow noreferrer">C</a> (Arduino) 8-bit byte array to a 16-bit int array, but it only seems to partially work. I'm not sure what I'm doing wrong.</p>
  
  <p>The byte array is in little endian byte order. How do I convert it to an int (two bytes per enty) array?</p>
  
  <p>In layman's terms, I want to merge every two bytes.</p>
  
  <p>Currently it is outputting for an input BYTE ARRAY of: <code>{0x10, 0x00, 0x00, 0x00, 0x30, 0x00}</code>. The output INT ARRAY is: <code>{1,0,0}</code>. The output should be an INT ARRAY is: <code>{1,0,3}</code>.</p>
  
  <p>The code below is what I currently have:</p>
  
  <p>I wrote this function based on <a href="https://stackoverflow.com/questions/11295728/convert-bytes-in-a-c-array-as-longs/11295756#11295756">a solution in Stack Overflow question <em>Convert bytes in a C array as longs</em></a>.</p>
  
  <p>I also have this solution based off the same code which works fine for byte array to long (32-bits) array <code>http://pastebin.com/TQzyTU2j</code>.</p>
  
  <pre><code>/**
  * Convert the retrieved bytes into a set of 16 bit ints
  **/
  int * byteA2IntA(byte * byte_slice, int sizeOfB, int * ret_array){
  
      //Variable that stores the addressed int to be stored in SRAM
      int currentInt;
      int sizeOfI = sizeOfB / 2;
  
      if(sizeOfB % 2 != 0) ++sizeOfI;
          for(int i = 0; i &lt; sizeOfB; i+=2){
              currentInt = 0;
              if(byte_slice[i]=='\0') {
                  break;
              }
              if(i + 1 &lt; sizeOfB)
                  currentInt = (currentInt &lt;&lt; 8) + byte_slice[i+1];
              currentInt = (currentInt &lt;&lt; 8) + byte_slice[i+0];
              *ret_array = currentInt;
              ret_array++;
          }
          //Pointer to the return array in the parent scope.
          return ret_array;
      }
  </code></pre>