Here you can find the source of intToBytes16(int sample, byte[] buffer, int byteOffset, boolean bigEndian)
int
to 2 bytes in an array.
public static void intToBytes16(int sample, byte[] buffer, int byteOffset, boolean bigEndian)
//package com.java2s; public class Main { /**//from w ww. j a va 2 s. c o m * Converts a 16 bit sample of type <code>int</code> to 2 bytes in an array. * <code>sample</code> is interpreted as signed (as Java does). * <p> * For little endian, buffer[byteOffset] is filled with low byte of sample, * and buffer[byteOffset+1] is filled with high byte of sample + sign bit. * <p> For big endian, this is reversed. * <p> Before calling this function, it should be assured that <code>sample</code> * is in the 16bit range - it will not be clipped. * <p> This is a reference function. */ public static void intToBytes16(int sample, byte[] buffer, int byteOffset, boolean bigEndian) { if (bigEndian) { buffer[byteOffset++] = (byte) (sample >> 8); buffer[byteOffset] = (byte) (sample & 0xFF); } else { buffer[byteOffset++] = (byte) (sample & 0xFF); buffer[byteOffset] = (byte) (sample >> 8); } } }