ShadowEmbrace

Binary Decoding

Feb 6th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   let string = document.getElementById('str').value;
  3.   let result = document.getElementById('result');
  4.  
  5.   let slicedString = string.slice(sum(string), -(sum(string)));
  6.  
  7.   let text = slicedString
  8.       .split(/(\d{8})/)
  9.       .filter(x => x)
  10.       .map(a => binaryToString(a))
  11.       .filter(b => /[A-Za-z ]+/g.test(b))
  12.       .join('');
  13.  
  14.   result.textContent = text;
  15.  
  16.   function sum(str) {
  17.       let result = str;
  18.  
  19.     while (result.length > 1) {
  20.         let temp = result
  21.             .split('')
  22.             .reduce((total, num) => +total + +num)
  23.             .toString();
  24.         result = temp;
  25.     }
  26.     return +result;
  27.   }
  28.  
  29.   function binaryToString(e) {
  30.     let decimal = parseInt(e, 2);
  31.     return String.fromCharCode(decimal);
  32.   }
  33. }
Add Comment
Please, Sign In to add comment