将ByteArray转换为IntArray java
我有一个ByteArrayOutputStream
与AudioSource的Dataline连接。 我需要将Stream转换为一些有意义的值,这些值很可能是从源获取的声音值? 那么我怎样才能在intArray中收敛byteArray(来自ByteArrayOutStream.getByteArray()
)? 我用Google搜索但没有运气。
ps我使用的audioFormat是:PCM_SIGNED 192.0Hz 16Bit big endian
使用ByteBuffer
。 您不仅可以通过这种方式转换为不同的数组类型,还可以处理字节序问题。
您可以尝试以下方法:
ByteBuffer.wrap(byteArray).asIntBuffer().array()
当你做ByteArrayOutStream.toByteArray()
,得到: byte[]
。 所以现在,我假设您需要将byte []转换为int。
你可以这样做:
/** * 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; }