Skip to content

Instantly share code, notes, and snippets.

@brawaru
Created August 29, 2018 21:16
Show Gist options
  • Select an option

  • Save brawaru/49fdb0ff39e956867c205327a6ad03ad to your computer and use it in GitHub Desktop.

Select an option

Save brawaru/49fdb0ff39e956867c205327a6ad03ad to your computer and use it in GitHub Desktop.
Example of Binary-to-Decimal conversion function for JavaScript with BigInt support
function bin2dec(b) {
if (!/^[0-1]+$/.test(b)) {
throw new Error("Invalid binary value");
}
let d = 0n;
for (let i = 0, l = b.length; i < l; i++) {
d = d * 2n;
if (b[i] === "1") {
d += 1n;
}
}
return d + "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment