Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/core/lib/Binary.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import Utils from "../Utils.mjs";
import OperationError from "../errors/OperationError.mjs";

export const MAX_BINARY_PADDING = 65536;

/**
* Convert a byte array into a binary string.
Expand All @@ -32,6 +33,10 @@ 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");

padding = Number(padding);
if (!Number.isFinite(padding) || padding < 0 || Math.round(padding) !== padding || padding > MAX_BINARY_PADDING)
throw new OperationError(`Byte length must be an integer between 0 and ${MAX_BINARY_PADDING}`);

delim = Utils.charRep(delim);
let output = "";

Expand Down Expand Up @@ -77,4 +82,3 @@ export function fromBinary(data, delim="Space", byteLen=8) {
}
return output;
}

4 changes: 3 additions & 1 deletion src/core/operations/ToBinary.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import Operation from "../Operation.mjs";
import Utils from "../Utils.mjs";
import {BIN_DELIM_OPTIONS} from "../lib/Delim.mjs";
import {toBinary} from "../lib/Binary.mjs";
import {MAX_BINARY_PADDING, toBinary} from "../lib/Binary.mjs";

/**
* To Binary operation
Expand Down Expand Up @@ -35,6 +35,8 @@ class ToBinary extends Operation {
{
"name": "Byte Length",
"type": "number",
"min": 1,
"max": MAX_BINARY_PADDING,
"value": 8
}
];
Expand Down
1 change: 1 addition & 0 deletions tests/operations/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ import "./tests/TakeNthBytes.mjs";
import "./tests/Template.mjs";
import "./tests/TextEncodingBruteForce.mjs";
import "./tests/TextIntegerConverter.mjs";
import "./tests/ToBinary.mjs";
import "./tests/ToFromInsensitiveRegex.mjs";
import "./tests/TranslateDateTimeFormat.mjs";
import "./tests/Typex.mjs";
Expand Down
33 changes: 33 additions & 0 deletions tests/operations/tests/ToBinary.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* To Binary tests
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2026
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";

TestRegister.addTests([
{
name: "To Binary: custom byte length",
input: "hello",
expectedOutput: "01101000 01100101 01101100 01101100 01101111",
recipeConfig: [
{
"op": "To Binary",
"args": ["Space", 8]
}
]
},
{
name: "To Binary: byte length too large",
input: "hello",
expectedOutput: "Byte length must be an integer between 0 and 65536",
recipeConfig: [
{
"op": "To Binary",
"args": ["Space", "8111111111111111111"]
}
]
}
]);