Skip to content

Instantly share code, notes, and snippets.

@jadsongmatos
Created August 26, 2021 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadsongmatos/a9d21c27be5a145df752945db34855c6 to your computer and use it in GitHub Desktop.
Save jadsongmatos/a9d21c27be5a145df752945db34855c6 to your computer and use it in GitHub Desktop.
const vec = [0,1,2,4,8,16,32,64,128];
const string = compressVec(vec);
const decompress = decompressVec(string);
function compressVec(vec) {
let result = "";
for (let i = 0; i < vec.length; i++) {
// parseInt("1"+String("0000000" + vec[i].toString(2)).slice(-7), 2) == vec[i] + 128 ?
result = result + String.fromCharCode(vec[i] + 128);
}
return result;
}
function decompressVec(string) {
if (string) {
let result = [];
for (let i = 0; i < string.length; i++) {
//parseInt("0" + string[i].charCodeAt().toString(2).substring(1), 2) == string[i].charCodeAt() - 128 ?
result.push(string[i].charCodeAt() - 128);
}
return result;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment