我已经尝试过this short2byte and byte2short conversion, works fine but time consuming.

我正在将字节转换为short,如下所示

ByteBuffer.wrap(bData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(sData);


我想以同样的方式将short转换回byte。

我已经搜索并看到了许多示例,但并没有按我的意愿获得。

最佳答案

我想我知道了。

使用ByteBuffer将字节转换为short

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


使用ByteBuffer转换为字节到字节

// to turn shorts back to bytes.
byte[] bData= new byte[sData.length * 2];
ByteBuffer.wrap(bData).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().put(sData);

10-05 23:28
查看更多