title: Reading in WAV data using Java - Endianness is not working for some values?
body: <p>I am using Java to read in WAV data. With the WAV data being little-endian and Java using big-endian, I was looking to convert the values upon reading them in, as shown below, which I will then manipulate.</p>
  
  <p>However, it seems to be working only partially for certain values.
  The first image shows the original WAV data, which should be read in as <code>98 00</code> and then swapped, to get <code>00 98</code>, which would equate to <code>152</code> in decimal. My code below is however receiving the value <code>-104</code> in decimal, which eqautes to simply <code>98</code> in hex. Note that this code should be able to take in negative values, as this is important to the use of the WAV data once it is read in.</p>
  
  <p>This can equally seen a bit further down the same line as the higlighted values when the first value in the byte is above 80. </p>
  
  <p><a href="https://i.stack.imgur.com/qzt49.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/qzt49.png" alt="Original wav sample"></a>
  <a href="https://i.stack.imgur.com/cgVH4.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/cgVH4.png" alt="Read in and saved wav sample"></a></p>
  
  <pre><code>public boolean read (String filename) {
  
      try {
          FileInputStream input = new FileInputStream(new File(filename));
  
          if (!(riff_header.read(input))) {
              return false;
          }
  
          nSamples = (riff_header.getData_size() / riff_header.getBytes_samp());
          System.out.println("Number of samples: " + nSamples);
  
          sample = new int[nSamples];
          System.out.println("Sizeof sample: " + sample.length);
  
          // Should sample two bytes at a time (if 1 channel WAV)
          byte[] bytes = new byte[riff_header.getBytes_samp()];
          // incrementing by bytes_samp every iteration ( in this case, for test_file.wav = 2 bytes)
          for (int i = 0; i &lt; riff_header.getData_size(); i+=riff_header.getBytes_samp()) {
  
  
              if (i / riff_header.getBytes_samp() == 402) {
                  System.out.println("break");
              }
  
              input.read(bytes);
              // note - working backwards through array - endian order
              int joined = 0;
              for (int j = 0; j &lt; bytes.length; j++) {
                  joined = joined | (int) (((bytes[(bytes.length - 1) - j]) &lt;&lt; ((bytes.length - 1) - j) * 8));
              }
  
              sample[i / riff_header.getBytes_samp()] = joined;
  
          } 
          input.close();
  
          return true;
  </code></pre>
  
  <p>Howw</p>