【问题标题】:Conversion shortarray to bytearray & bytearray to shortarray将 shortarray 转换为 bytearray 和 bytearray 到 shortarray
【发布时间】:2012-11-26 07:57:30
【问题描述】:

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

我正在将 byte 转换为 short 如下所示

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

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

我已经搜索并查看了许多示例,但没有得到我想要的。

【问题讨论】:

  • 我认为您无法比这更快地获得转换。
  • 这种转换只查看每个短片一次,并对其进行非常有效的按位运算。
  • @Cruncher 你对使用 ByteBuffer 有什么看法??我认为这需要更少的时间

标签: java android bytearray short


【解决方案1】:

我想我明白了。

使用 ByteBuffer 将字节转换为短字节

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 2014-04-28
    • 1970-01-01
    • 1970-01-01
    • 2019-12-13
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多