Opérations élémentaires :
console.log(/^0b[01]+$/i.test("0b0011"));
console.log(/^0b[01]+$/i.test("0b0013"));
console.log("0b0011".slice(2));
console.log(parseInt("0011", false ? 2 : 8))
console.log("0o0011".slice(2));
console.log(parseInt("0011", true ? 2 : 8))
console.log()
let reTrim = /^\s+|\s+$/g,
reIsBinary = /^0b[01]+$/i,
reIsOctal = /^0o[0-7]+$/i,
reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
function toNumber(value) {
if (typeof value == 'number') {
return value;
}
if (typeof value != 'string') {
return value === 0 ? value : +value;
}
value = value.replace(reTrim, '');
let isBinary = reIsBinary.test(value);
return (isBinary || reIsOctal.test(value))
? parseInt(value.slice(2), isBinary ? 2 : 8)
: (reIsBadHex.test(value) ? NAN : +value);
}
console.log( toNumber(" 0b0011 "))
code
Range
range(0b011, "10", 1)
[ 3, 4, 5, 6, 7, 8, 9 ]
range(" 0b011 ", " 000005", "NON")
[ 3, 3 ]
range(" 0b011 ", " 000005", " 0o1")
[ 3, 4 ]
code