UNPKG

664 BJavaScriptView Raw
1/**
2 * @name u8aToFloat
3 * @description Converts a Uint8Array value into the float (either 32 or 64-bit)
4 * representation.
5 */
6export 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}