Created
December 18, 2022 12:45
-
-
Save mkg20001/bf17ce6fd2b251473894f10706a9bfb6 to your computer and use it in GitHub Desktop.
Node functions to decode and encode strings into literal binary format
This file contains 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
// Decode binary | |
function decodeBinary(arr) { | |
let out = '' | |
for (let i = 0; i < arr.length; i += 8) { | |
out += String(Buffer.from(parseInt(arr.slice(i, i + 8), 2).toString(16), 'hex')) | |
} | |
return out | |
} | |
function encodeBinary(str) { | |
let out = '' | |
for (let i = 0; i < str.length; i++) { | |
let bin = str.charCodeAt(i).toString(2) | |
out += '0'.repeat(8 - bin.length) + bin | |
} | |
return out | |
} | |
console.log(decodeBinary(encodeBinary( | |
'You can use this decodeBinary function to get the *secret* messages from r/ProgrammerHumor memes or whatever' | |
))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment