mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 14:56:19 -04:00
53 lines
1.1 KiB
JavaScript
53 lines
1.1 KiB
JavaScript
![]() |
/**
|
||
|
* Numerical base operations.
|
||
|
*
|
||
|
* @author n1474335 [n1474335@gmail.com]
|
||
|
* @copyright Crown Copyright 2016
|
||
|
* @license Apache-2.0
|
||
|
*
|
||
|
* @namespace
|
||
|
*/
|
||
|
var Base = {
|
||
|
|
||
|
/**
|
||
|
* @constant
|
||
|
* @default
|
||
|
*/
|
||
|
DEFAULT_RADIX: 36,
|
||
|
|
||
|
/**
|
||
|
* To Base operation.
|
||
|
*
|
||
|
* @param {number} input
|
||
|
* @param {Object[]} args
|
||
|
* @returns {string}
|
||
|
*/
|
||
|
run_to: function(input, args) {
|
||
|
if (!input) {
|
||
|
throw ("Error: Input must be a number");
|
||
|
}
|
||
|
var radix = args[0] || Base.DEFAULT_RADIX;
|
||
|
if (radix < 2 || radix > 36) {
|
||
|
throw "Error: Radix argument must be between 2 and 36";
|
||
|
}
|
||
|
return input.toString(radix);
|
||
|
},
|
||
|
|
||
|
|
||
|
/**
|
||
|
* From Base operation.
|
||
|
*
|
||
|
* @param {string} input
|
||
|
* @param {Object[]} args
|
||
|
* @returns {number}
|
||
|
*/
|
||
|
run_from: function(input, args) {
|
||
|
var radix = args[0] || Base.DEFAULT_RADIX;
|
||
|
if (radix < 2 || radix > 36) {
|
||
|
throw "Error: Radix argument must be between 2 and 36";
|
||
|
}
|
||
|
return parseInt(input.replace(/\s/g, ""), radix);
|
||
|
},
|
||
|
|
||
|
};
|