title: Converting 16BitPCM to .wav, after switching endianness, the .wav file plays backwards
body: <p>I am trying to build an Android app that records PCM audio and exports it as a wav file.</p>
  <p>It worked fine for 8BitPCM, but when I switched to 16BitPCM I got white noise.</p>
  <p>I finally figured out it was the endianness of the byte array, but now, after converting from Little Endian to Big Endian, I get my audio crystal clear, but reversed!</p>
  <p>Here is how I call the method:</p>
  <p><code>byte[] inputByteArray = convertLittleEndianToBig(readToByte(input));</code></p>
  <p>and then that byte[] is appended to my .wav header here:</p>
  <pre><code>        OutputStream os;
          os = new FileOutputStream(output);
          BufferedOutputStream bos = new BufferedOutputStream(os);
          DataOutputStream outFile = new DataOutputStream(bos);
  
          // Adding header here...
  
          outFile.write(inputByteArray);
  </code></pre>
  <p>convertLittleEndianToBig():</p>
  <pre><code>   public static byte[] convertLittleEndianToBig(byte[] value) {
      final int length = value.length;
      byte[] res = new byte[length];
      for(int i = 0; i &lt; length; i++) {
          res[length - i - 1] = value[i];
      }
      return res;
  }
  </code></pre>
  <p>and.... readToByte():</p>
  <pre><code>public static byte[] readToByte(File file) throws IOException, FileNotFoundException {
      if (file.length() &lt; MAX_FILE_SIZE &amp;&amp; file.length() != 0L) {
          ByteArrayOutputStream ous = null;
          InputStream ios = null;
          try {
              byte[] buffer = new byte[4096];
              ous = new ByteArrayOutputStream();
              ios = new FileInputStream(file);
              int read = 0;
              while ((read = ios.read(buffer)) != -1) {
                  ous.write(buffer, 0, read);
              }
          } finally {
              try {
                  if (ous != null)
                      ous.close();
              } catch (IOException e) {
              }
  
              try {
                  if (ios != null)
                      ios.close();
              } catch (IOException e) {
              }
          }
          return ous.toByteArray();
      }
      else {
      return new byte[0];
      }
  </code></pre>
  <p>So weird that the audio sounds exactly right, but backwards.</p>
  <p>If I remove the call to &quot;convertLittleEndianToBig()&quot; I am back to white noise static.</p>
  <p>Thanks for any help. This is my first real project.</p>