Skip to content

Instantly share code, notes, and snippets.

@deadManAlive
Last active June 27, 2023 14:15
Show Gist options
  • Save deadManAlive/639d4edc091884cf03e780c2e0ecd839 to your computer and use it in GitHub Desktop.
Save deadManAlive/639d4edc091884cf03e780c2e0ecd839 to your computer and use it in GitHub Desktop.
binary to decimal
const b2h: (string) => string = (bin: string) => bin.split("")
.reverse()
.reduce((p, c, i, a) => {
switch(i % 4 === 0) {
case true:
return p.concat(a.slice(i, i + 4).join(""));
default:
return p;
}
}, <string[]>[])
.reverse()
.map(v => v.split("")
.reduce((p, c, i, a) => p + (+c * Math.pow(2, i)), 0)
.toString(16))
.join("")
.toUpperCase();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment