Last active
November 15, 2017 13:52
-
-
Save SethVandebrooke/52d4a27b487cdd4701f3f1d12c1e0765 to your computer and use it in GitHub Desktop.
Compress small Ints based off a character string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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