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