以下是将byte[]转换为short[]的接口:

public short[] byteToShort(byte[] bytes) {
    int length = bytes.length;
    short[] shorts = new short[length / 2];
    for (int i = 0; i < length / 2; i++) {
        shorts[i] = (short) ((bytes[i * 2] & 0xFF) | (bytes[i * 2 + 1] << 8));
    }
    return shorts;
}

以下是将short[]转换为byte[]的接口:

public byte[] shortToByte(short[] shorts) {
    int length = shorts.length;
    byte[] bytes = new byte[length * 2];
    for (int i = 0; i < length; i++) {
        bytes[i * 2] = (byte) (shorts[i] & 0xFF);
        bytes[i * 2 + 1] = (byte) ((shorts[i] >> 8) & 0xFF);
    }
    return bytes;
}

这两个接口可以在Android开发中用于将byte[]转换为short[],或者将short[]转换为byte[]

Android开发:给个byte转short的接口和short转byte的接口

原文地址: https://www.cveoy.top/t/topic/iMhj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录