-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
76 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Specification JSBT v1.2.1 | ||
# Specification JSBT v1.2.3 | ||
JavaScript Byte Translation | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
|
||
export const MAX_7_BYTES_INTEGER = 0x1FFFFFFFFFFFFF; // Number.MAX_SAFE_NUMBER; | ||
export const MAX_7_BYTES_INTEGER = 0x1F_FF_FF_FF_FF_FF_FF; // Number.MAX_SAFE_NUMBER; | ||
export const MAX_DATE_INTEGER = 8640000000000000; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
import { bytesToBigInt } from './bytesToBigInt' | ||
|
||
export const bytesToInteger = (bytes: Uint8Array | number[]): number => { | ||
return Number(bytesToBigInt(bytes)); | ||
const len = Array.isArray(bytes) ? bytes.length : bytes.byteLength; | ||
if (len === 0) { | ||
return 0; | ||
} | ||
|
||
let int = 0; | ||
for (let i = 0; i < len; i += 1) { | ||
const byte = bytes[i]; | ||
int = (byte * (256**i)) + int | ||
} | ||
|
||
return int; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,14 @@ | ||
import { bigIntToBytes } from '../converter/bigIntToBytes'; | ||
|
||
export const integerToBytes = (int: number, byteSize: number = 0, bigEndianOrder: boolean = false): number[] => { | ||
if (int > 0xFF_FF_FF_FF || int <= -0xFF_FF_FF_FF) { | ||
// Because JS uses 4 bytes for bitwise operations :( | ||
return bigIntToBytes( | ||
BigInt(int), | ||
int < 0 && !byteSize ? 8 : byteSize, | ||
bigEndianOrder, | ||
).map((bint) => Number(bint)); | ||
if (int < 0) { | ||
throw new Error('integerToBytes does not support negative integers'); | ||
} | ||
const bytes: number[] = []; | ||
let num = int; | ||
let num = Math.abs(int); | ||
for (let i = 1; byteSize ? i <= byteSize : num; i += 1) { | ||
bytes.push(num & 0xFF); | ||
num = num >>> 8; | ||
const n = num % 256; | ||
bytes.push(n); | ||
num = num - n; | ||
num = Math.floor(num / 256); | ||
} | ||
return bigEndianOrder ? bytes.reverse() : bytes; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters