byte array to short array and back again in java

后端 未结 6 1636
时光取名叫无心
时光取名叫无心 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:41

    Your code is doing little-endian shorts, not big. You've the indexing for MSB and LSB swapped.

    Since you are using big-endian shorts, you could be using a DataInputStream wrapped around a ByteArrayInputStream (and DataOutputStream/ByteArrayOutputStream) on the other end, rather than doing your own decoding.

    If you're getting every other decode working, I'd guess you've got an odd number of bytes, or an off-by-one error elsewhere which is causing your mistake to get fixed on every other pass.

    Finally, I'd step through the array with i+=2 and use MSB= arr[i] and LSB=arr[i+1] rather than multiplying by 2, but that's just me.

提交回复
热议问题