byte array to short array and back again in java

后端 未结 6 1615
时光取名叫无心
时光取名叫无心 2020-11-27 03:50

I\'m having some issues taking audio data stored in a byte array, converting it to a big-endian short array, encoding it, then changing it back into a byte array. Here is wh

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 04:48

    I also suggest you try ByteBuffer.

    byte[] bytes = {};
    short[] shorts = new short[bytes.length/2];
    // to turn bytes to shorts as either big endian or little endian. 
    ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
    
    // to turn shorts back to bytes.
    byte[] bytes2 = new byte[shortsA.length * 2];
    ByteBuffer.wrap(bytes2).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(shortsA);
    

提交回复
热议问题