1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | export function u8aToFloat(value, { bitLength = 32, isLe = true } = {}) {
|
7 | if (bitLength !== 32 && bitLength !== 64) {
|
8 | throw new Error('Invalid bitLength provided, expected 32 or 64');
|
9 | }
|
10 | else if (value.length < (bitLength / 8)) {
|
11 | throw new Error(`Invalid input buffer provided, expected at least ${bitLength / 8} bytes, found ${value.length}`);
|
12 | }
|
13 | const dv = new DataView(value.buffer, value.byteOffset);
|
14 | return bitLength === 32
|
15 | ? dv.getFloat32(0, isLe)
|
16 | : dv.getFloat64(0, isLe);
|
17 | }
|