Java put / putShort with JS

Can someone tell me the equivalent of java put and putShort in JavaScript ?

int length = (bArr.length + 2) - 4;
ByteBuffer wrap = ByteBuffer.wrap(bArr);
wrap.order(ByteOrder.LITTLE_ENDIAN);
wrap.putShort(2, (short) length);
wrap.put(9, length);
评论
对你的冷漠
对你的冷漠

我认为您首先要寻找的是:

  • 创建数组缓冲区:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer

  • 然后使用数据视图获取并设置它:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView

在dataview中,您可以控制字节顺序(大/小尾数)

And here is an example, getting binary representation of NaN in JS https://jsfiddle.net/ibowankenobi/h2r5ybfp/

!function(){
    var ab = new ArrayBuffer(8),
        dv = new DataView(ab);
  dv.setFloat64(0,NaN,false); //big endian by default
  document.body.textContent = Array.apply(null,Array(8))
    .map(function(d,i){
       return ("00000000" + dv.getUint8(i).toString(2)).slice(-8)
    }).join(" ");
}();
点赞
评论
加载中…
加载中…

I believe you are looking for ByteBuffer#writeShort(value, offset=)

点赞
评论