繁体   English   中英

将 ByteArray 转换为 IntArray java

[英]Convert ByteArray to IntArray java

我有一个ByteArrayOutputStream与来自 AudioSource 的数据线连接。 我需要将 Stream 转换为一些可能是从源获取的声音值的重要值吗? 那么如何在 intArray 中转换 byteArray(来自ByteArrayOutStream.getByteArray() )? 我用谷歌搜索但没有运气。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM