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

棘丶
12 声望1 粉丝

引用和评论

0 条评论