Last active
February 25, 2020 02:47
-
-
Save wbamberg/27d6693c6f91ed0f8a094cea782b9f0a to your computer and use it in GitHub Desktop.
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
"use strict"; | |
function toBinary(string) { | |
const codeUnits = new Uint16Array(string.length); | |
for (let i = 0; i < codeUnits.length; i++) { | |
codeUnits[i] = string.charCodeAt(i); | |
} | |
return String.fromCharCode(...new Uint8Array(codeUnits.buffer)); | |
} | |
function fromBinary(binary) { | |
const bytes = new Uint8Array(binary.length); | |
for (let i = 0; i < bytes.length; i++) { | |
bytes[i] = binary.charCodeAt(i); | |
} | |
return String.fromCharCode(...new Uint16Array(bytes.buffer)); | |
} | |
const myString = "☸☹☺☻☼☾☿"; | |
const converted = toBinary(myString); | |
const encoded = btoa(converted); | |
console.log(encoded); | |
const decoded = atob(encoded); | |
const original = fromBinary(decoded); | |
console.log(original); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment