Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Key Compressor

Yumi Murakami
DoIt!AttachTheEarOfACat!
Join date: 27 Sep 2005
Posts: 6,860
11-16-2005 15:54
Cross-posted from tips: two functioms to compress keys to reduce the memory requirements if you need to make large lists of them. Reduces a key from 36 to 22 characters, so you can store roughly 40% more in the same memory. Yays!

Note that the forum may have inserted spaces in the strings near the start - there should be no spaces in them, and no returns or anything similar. Also - technically chref doesn't need to be that long, but I left those in for future expansion to maybe actually do some proper data compression. :)

CODE

// 4 bits per char, boo.
string hexchars = "0123456789abcdef";

// 6 bits per char, yay!

string chref = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()[]{}?+-_\|',.<>~`;:";

string crunchkey(key thekey) {
integer t;
string skey = (string)thekey;
integer buffbits = 0;
integer buffval = 0;
string outkey = "";

for (t=0; t<36; t++) {
string curchar = llGetSubString(skey,t,t);
if (curchar != "-") {
integer curval = llSubStringIndex(hexchars,curchar);

buffval+=(curval << buffbits);
buffbits+=4;
if (buffbits > 5) {
integer lval = buffval & 63;
outkey += llGetSubString(chref,lval,lval);
buffval = buffval >> 6;
buffbits -= 6;
}
}
}
outkey += llGetSubString(chref,buffval,buffval);
return outkey;
}

key uncrunchkey(string crunched) {
integer t;
string outkey;
integer buffbits = 0;
integer buffval = 0;
integer outchars = 0;

for (t=0; t<22; t++) {
string curchar = llGetSubString(crunched,t,t);
integer curval = llSubStringIndex(chref,curchar);
buffval += (curval << buffbits);
buffbits += 6;
while (buffbits > 3) {
integer lval = buffval & 15;
if (outchars < 32) outkey += llGetSubString(hexchars,lval,lval);
outchars++;
if (outchars == 8) outkey += "-";
if (outchars == 12) outkey += "-";
if (outchars == 16) outkey += "-";
if (outchars == 20) outkey += "-";
buffval = buffval >> 4;
buffbits -= 4;
}

}
return (key)outkey;
}
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Discussion Thread
11-17-2005 18:00
/54/8a/72317/1.html
_____________________
i've got nothing. ;)
Ollj Oh
Registered User
Join date: 28 Aug 2007
Posts: 522
12-25-2007 00:34
thats funny because I store 31 bit in a double-char.