Convert ByteArray to IntArray java

I have a ByteArrayOutputStreamDataline related from AudioSource. Do I need to convert the stream to some meaningful values, which are probably sound values ​​taken from the source or not? So how can I convert byteArray (from ByteArrayOutStream.getByteArray()) to intArray ?. I was looking for her, but no luck.

ps the audio format that I used: PCM_SIGNED 192.0Hz 16Bit big endian

+3
source share
3 answers

Use ByteBuffer. You can convert not only to different types of arrays in this way, but also deal with endian problems .

+7
source

You can try the following:

ByteBuffer.wrap(byteArray).asIntBuffer().array()
+3
source

ByteArrayOutStream.toByteArray(), : byte[]. , , [] int.

You can do this :

/**
 * Convert the byte array to an int.
 *
 * @param b The byte array
 * @return The integer
 */
public static int byteArrayToInt(byte[] b) {
    return byteArrayToInt(b, 0);
}

/**
 * Convert the byte array to an int starting from the given offset.
 *
 * @param b The byte array
 * @param offset The array offset
 * @return The integer
 */
public static int byteArrayToInt(byte[] b, int offset) {
    int value = 0;
    for (int i = 0; i < 4; i++) {
        int shift = (4 - 1 - i) * 8;
        value += (b[i + offset] & 0x000000FF) << shift;
    }
    return value;
}
+1
source

All Articles