Skip to content

Instantly share code, notes, and snippets.

@SethVandebrooke
Last active November 15, 2017 13:52
Show Gist options
  • Save SethVandebrooke/52d4a27b487cdd4701f3f1d12c1e0765 to your computer and use it in GitHub Desktop.
Save SethVandebrooke/52d4a27b487cdd4701f3f1d12c1e0765 to your computer and use it in GitHub Desktop.
Compress small Ints based off a character string
function intToCCV(n,c) { // Character compression value
var c = c || "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890!?";
var r = [0,0,0,0];
while (n - 64 >= 0) {
var pos = 2;
r[r.length-pos] += 1;
while (r[r.length-pos]==c.length) {
r[r.length-pos] = 0;
pos += 1; // Go to next position
if (r.length-pos in r) { // If that position is alread in the data
r[r.length-pos] += 1; // Increment that position
} else {
r.unshift(1); // Otherwise add it and set it to 1
}
}
n-=64;
}
r[r.length-1] = n;
var f = "";
r.forEach(function(e){
f += c.charAt(e);
});
return f;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment