-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 'Binary' key option to all bitwise operations. Closes #398
- Loading branch information
Showing
10 changed files
with
97 additions
and
34 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
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 |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* Binary functions. | ||
* | ||
* @author n1474335 [[email protected]] | ||
* @copyright Crown Copyright 2018 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Utils from "../Utils"; | ||
|
||
|
||
/** | ||
* Convert a byte array into a binary string. | ||
* | ||
* @param {Uint8Array|byteArray} data | ||
* @param {string} [delim="Space"] | ||
* @param {number} [padding=8] | ||
* @returns {string} | ||
* | ||
* @example | ||
* // returns "00010000 00100000 00110000" | ||
* toHex([10,20,30]); | ||
* | ||
* // returns "00010000 00100000 00110000" | ||
* toHex([10,20,30], ":"); | ||
*/ | ||
export function toBinary(data, delim="Space", padding=8) { | ||
if (!data) return ""; | ||
|
||
delim = Utils.charRep(delim); | ||
let output = ""; | ||
|
||
for (let i = 0; i < data.length; i++) { | ||
output += data[i].toString(2).padStart(padding, "0") + delim; | ||
} | ||
|
||
if (delim.length) { | ||
return output.slice(0, -delim.length); | ||
} else { | ||
return output; | ||
} | ||
} | ||
|
||
|
||
/** | ||
* Convert a binary string into a byte array. | ||
* | ||
* @param {string} data | ||
* @param {string} [delim] | ||
* @param {number} [byteLen=8] | ||
* @returns {byteArray} | ||
* | ||
* @example | ||
* // returns [10,20,30] | ||
* fromBinary("00010000 00100000 00110000"); | ||
* | ||
* // returns [10,20,30] | ||
* fromBinary("00010000:00100000:00110000", "Colon"); | ||
*/ | ||
export function fromBinary(data, delim="Space", byteLen=8) { | ||
const delimRegex = Utils.regexRep(delim); | ||
data = data.replace(delimRegex, ""); | ||
|
||
const output = []; | ||
for (let i = 0; i < data.length; i += byteLen) { | ||
output.push(parseInt(data.substr(i, byteLen), 2)); | ||
} | ||
return output; | ||
} | ||
|
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
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
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
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