Last active
June 27, 2023 14:15
-
-
Save deadManAlive/639d4edc091884cf03e780c2e0ecd839 to your computer and use it in GitHub Desktop.
binary to decimal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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