Skip to content

Instantly share code, notes, and snippets.

@Nnubes256
Last active February 10, 2017 23:09
Show Gist options
  • Save Nnubes256/dfb6f1887edb1fab7683a95672241cb8 to your computer and use it in GitHub Desktop.
Save Nnubes256/dfb6f1887edb1fab7683a95672241cb8 to your computer and use it in GitHub Desktop.
BaseLea implementation
// TODO:
// - Add input validation
// - Improve reliability of atob parsing
var TABLE = [
"Hi.","Hi?","Hi!","Lea.", // 0...4
"Lea?","Lea!","Bye.","Bye?", // 5...8
"Bye!","How.","How?","How!", // 9...B
"Why.","Why?","Why!","..." // C...F
];
function baselea_btoa(input) {
var output = [];
for (var i = 0; i < input.length; i++) {
var charcode = input.charCodeAt(i);
output.push(TABLE[charcode & 0xF]); // Low nibble
output.push(TABLE[charcode >> 4]); // High nibble
}
return output.join(" ");
}
function baselea_atob(input) {
var input_split = input.split(" ");
var output = [];
for (var i = 0; i < input_split.length; i+=2) {
var low = TABLE.indexOf(input_split[i]);
var high = TABLE.indexOf(input_split[i+1]);
output.push(String.fromCharCode((high << 4) + low));
}
return output.join("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment