乐趣区

关于jav:short数组转发byte数组

short 数组转发 byte 数组并可实现位置换高位

byte[] tobytes(short[] shorts, boolean bigendian) {
    int n = 0;
    byte[] bytes = new byte[2*shorts.length];
 
    for (n=0; n < shorts.length; n++) {byte lsb = shorts[n] & 0xff;
        byte msb = (shorts[n] >> 8) & 0xff;
        if (bigendian) {bytes[2*n]   = msb;
            bytes[2*n+1] = lsb;
        } else {bytes[2*n]   = lsb;
            bytes[2*n+1] = msb;
        }
    }
    return bytes;
}
退出移动版