在以下示例中,我获取 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 整数吗?
在以下示例中,我获取 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(new DataView(result.value.buffer).getUint16()));
请参阅ReadableStreamDefaultReader.read()
、Uint8Array
和DataView
。
或者更简单的方法:
fetch('https://www.random.org/cgi-bin/randbyte?nbytes=2')
.then(response => response.arrayBuffer())
.then(buffer => console.log(new DataView(buffer).getUint16()));