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

你可能感兴趣的:(jav)