Convert byte [] to short [] in Java

Possible duplicate:
> byte array for short array and back in java

The encodeAudio() method in Xuggler has the following parameters:

int streamIndes short [] samples longtimeStamp Unit TimeUnit
Using TargetDataLine from javax.sound.sampled , I can read the data into a byte[] array
 byte[] tempBuffer = new byte[10000]; fromMic.read(tempBuffer,0,tempBuffer.length); 

But the problem is that the samples argument requires short[]

+4
source share
1 answer

You are fortunate that byte "completely stripped" to short , so:

 // Grab size of the byte array, create an array of shorts of the same size int size = byteArray.length; short[] shortArray = new short[size]; for (int index = 0; index < size; index++) shortArray[index] = (short) byteArray[index]; 

And then use shortArray .

Note. As for the primitive type, Java always processes them in a large order, so converting, say, the ff byte will reduce 00ff .

+9
source

All Articles