import { ERRORTYPES as error } from "./error";
Copyright 2015-2021 JC Fisher
import { ERRORTYPES as error } from "./error";
BIN2DEC converts binary string into decimal value
export default function bin2dec(value) {
var valueAsString;
if (typeof value === "string") {
valueAsString = value;
} else if (typeof value !== "undefined") {
valueAsString = value.toString();
} else {
return error.NA;
}
if (valueAsString.length > 10) return error.NUM;
we subtract 512 when the leading number is 0.
if (valueAsString.length === 10 && valueAsString[0] === "1") {
return parseInt(valueAsString.substring(1), 2) - 512;
}
Convert binary number to decimal with built-in facility
return parseInt(valueAsString, 2);
}