Javascript 从可读流中获取Uint16

Javascript 从可读流中获取Uint16,javascript,random,uint16,Javascript,Random,Uint16,在下面的示例中,我获取2个原始字节 fetch('https://www.random.org/cgi-bin/randbyte?nbytes=2') .then(response=>response.body.getReader()) .then(reader=>0/*Here convert to Uint16*/) 知道如何从使用2个字节转换生成的可读流Uint16整数吗?如果您确定在第一个块中获得了所有需要的字节,那么您可以尝试以下方法: fetch('ht

在下面的示例中,我获取2个原始字节

fetch('https://www.random.org/cgi-bin/randbyte?nbytes=2')
    .then(response=>response.body.getReader())
    .then(reader=>0/*Here convert to Uint16*/)

知道如何从使用2个字节转换生成的可读流Uint16整数吗?

如果您确定在第一个块中获得了所有需要的字节,那么您可以尝试以下方法:

fetch('https://www.random.org/cgi-bin/randbyte?nbytes=2')
.then(response=>response.body.getReader())
.then(reader=>reader.read())
.then(result=>console.log(新数据视图(result.value.buffer).getUint16());
看,还有

或者更简单的方法:

fetch('https://www.random.org/cgi-bin/randbyte?nbytes=2')
.then(response=>response.arrayBuffer())
.then(buffer=>console.log(newdataview(buffer).getUint16());

一旦您完成了,就从它开始?顺便说一句,读取响应正文可能比使用可读的流读取器容易得多。