Java Integer to Byte Array intToBytes16(int sample, byte[] buffer, int byteOffset, boolean bigEndian)

Here you can find the source of intToBytes16(int sample, byte[] buffer, int byteOffset, boolean bigEndian)

Description

Converts a 16 bit sample of type int to 2 bytes in an array.

License

Open Source License

Declaration

public static void intToBytes16(int sample, byte[] buffer,
        int byteOffset, boolean bigEndian) 

Method Source Code

//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);
        }
    }
}

Related

  1. intToBytes(int value)
  2. intToBytes(int value)
  3. intToBytes(int value, byte[] array, int offset)
  4. intToBytes(int value, byte[] buffer, int offset)
  5. intToBytes(int value, byte[] buffer, int offset, int length, boolean littleEndian)
  6. IntToBytes4(int i)
  7. intToBytesBE(int value, byte[] buffer)
  8. intToBytesBigend(int value)
  9. intToBytesBigEndian(int n)