main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
995 bytes
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | var GetIntrinsic = require('get-intrinsic');
|
---|
4 |
|
---|
5 | var $pow = GetIntrinsic('%Math.pow%');
|
---|
6 | var $Number = GetIntrinsic('%Number%');
|
---|
7 | var $BigInt = GetIntrinsic('%BigInt%', true);
|
---|
8 |
|
---|
9 | module.exports = function bytesAsInteger(rawBytes, elementSize, isUnsigned, isBigInt) {
|
---|
10 | var Z = isBigInt ? $BigInt : $Number;
|
---|
11 |
|
---|
12 | // this is common to both branches
|
---|
13 | var intValue = Z(0);
|
---|
14 | for (var i = 0; i < rawBytes.length; i++) {
|
---|
15 | intValue += Z(rawBytes[i] * $pow(2, 8 * i));
|
---|
16 | }
|
---|
17 | /*
|
---|
18 | Let intValue be the byte elements of rawBytes concatenated and interpreted as a bit string encoding of an unsigned little-endian binary number.
|
---|
19 | */
|
---|
20 |
|
---|
21 | if (!isUnsigned) { // steps 5-6
|
---|
22 | // Let intValue be the byte elements of rawBytes concatenated and interpreted as a bit string encoding of a binary little-endian 2's complement number of bit length elementSize × 8.
|
---|
23 | var bitLength = elementSize * 8;
|
---|
24 |
|
---|
25 | if (rawBytes[elementSize - 1] & 0x80) {
|
---|
26 | intValue -= Z($pow(2, bitLength));
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | return intValue; // step 7
|
---|
31 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.