mirror of
https://github.com/gchq/CyberChef.git
synced 2025-05-07 23:15:14 -04:00
parent
de3ef202d5
commit
c5a1b69c30
693 changed files with 26685 additions and 51240 deletions
|
@ -9,6 +9,7 @@
|
|||
import Utils from "../Utils.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
|
||||
/**
|
||||
* Convert a byte array into a binary string.
|
||||
*
|
||||
|
@ -27,23 +28,19 @@ import OperationError from "../errors/OperationError.mjs";
|
|||
* // returns "1010:10100:11110"
|
||||
* toBinary([10,20,30], "Colon", 0);
|
||||
*/
|
||||
export function toBinary(data, delim = "Space", padding = 8) {
|
||||
export function toBinary(data, delim="Space", padding=8) {
|
||||
if (data === undefined || data === null)
|
||||
throw new OperationError(
|
||||
"Unable to convert to binary: Empty input data enocuntered",
|
||||
);
|
||||
throw new OperationError("Unable to convert to binary: Empty input data enocuntered");
|
||||
|
||||
delim = Utils.charRep(delim);
|
||||
let output = "";
|
||||
|
||||
if (data.length) {
|
||||
// array
|
||||
if (data.length) { // array
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
output += data[i].toString(2).padStart(padding, "0");
|
||||
if (i !== data.length - 1) output += delim;
|
||||
}
|
||||
} else if (typeof data === "number") {
|
||||
// Single value
|
||||
} else if (typeof data === "number") { // Single value
|
||||
return data.toString(2).padStart(padding, "0");
|
||||
} else {
|
||||
return "";
|
||||
|
@ -51,6 +48,7 @@ export function toBinary(data, delim = "Space", padding = 8) {
|
|||
return output;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Convert a binary string into a byte array.
|
||||
*
|
||||
|
@ -66,7 +64,7 @@ export function toBinary(data, delim = "Space", padding = 8) {
|
|||
* // returns [10,20,30]
|
||||
* fromBinary("00001010:00010100:00011110", "Colon");
|
||||
*/
|
||||
export function fromBinary(data, delim = "Space", byteLen = 8) {
|
||||
export function fromBinary(data, delim="Space", byteLen=8) {
|
||||
if (byteLen < 1 || Math.round(byteLen) !== byteLen)
|
||||
throw new OperationError("Byte length must be a positive integer");
|
||||
|
||||
|
@ -79,3 +77,4 @@ export function fromBinary(data, delim = "Space", byteLen = 8) {
|
|||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue