Skip to content

Instantly share code, notes, and snippets.

@wbamberg
Last active February 25, 2020 02:47
Show Gist options
  • Save wbamberg/27d6693c6f91ed0f8a094cea782b9f0a to your computer and use it in GitHub Desktop.
Save wbamberg/27d6693c6f91ed0f8a094cea782b9f0a to your computer and use it in GitHub Desktop.
"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