Skip to content

Instantly share code, notes, and snippets.

@RobertSkalko
Last active August 1, 2016 12:22
Show Gist options
  • Save RobertSkalko/13a4fe4147092b00a4c0fb39d6051386 to your computer and use it in GitHub Desktop.
Save RobertSkalko/13a4fe4147092b00a4c0fb39d6051386 to your computer and use it in GitHub Desktop.
function binaryAgent(str) {
var firstslice = [];
var lastslice = [];
var indexof1 = [];
var lastnumber = [];
var splitstr = str.split(" ");
var finalstr = [];
var binary = [1,10,11,100,101,110,111,1000,1001,1010,1011,1100,1101,1110,1111,
10000,10001, 10010,10011,10100,10101,10110,10111,11000,11001,11010,
11011,11100,11101,11110,11111];
// here we populate the variables at start, to use at next loop
for (var i = 0; i<splitstr.length;i++) {
firstslice.push(splitstr[i].slice(0,3));
lastslice.push(splitstr[i].slice(3));
indexof1.push(lastslice[i].indexOf(1));
lastnumber.push(lastslice[i].slice(indexof1[i]));
}
// here we loop through the splitstr to decode the binary numbers
for (var x = 0; x<splitstr.length;x++) {
if (firstslice[x]==='011') { // IF LOWERCASE
var count011 = 0;
count011 = 0;
for (var x011=0; x011<binary.length;x011++){
if (lastnumber[x] > binary[x011]) {count011++;}
}
finalstr.push(String.fromCharCode(97+count011));
}
if (firstslice[x]==='010') { // IF UPPERCASE
var count010 = 0;
count010 = 0;
for (var x010=0; x010<binary.length;x010++){
if (lastnumber[x] > binary[x010]) {count010++;}
}
finalstr.push(String.fromCharCode(65+count010));
}
if (firstslice[x]==='001') { // IF SYMBOL
var count001 = 0;
count001 = 0;
for (var x001=0; x001<binary.length;x001++){
if (lastnumber[x] >= binary[x001]) {count001++;}
}
finalstr.push(String.fromCharCode(32+count001));
}
} // end main loop
return finalstr.join("");
}
binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment