Skip to content

Instantly share code, notes, and snippets.

@mkg20001
Created December 18, 2022 12:45
Show Gist options
  • Save mkg20001/bf17ce6fd2b251473894f10706a9bfb6 to your computer and use it in GitHub Desktop.
Save mkg20001/bf17ce6fd2b251473894f10706a9bfb6 to your computer and use it in GitHub Desktop.
Node functions to decode and encode strings into literal binary format
// 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