Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Integer to Base64String

Adam Zaius
Deus
Join date: 9 Jan 2004
Posts: 1,483
08-14-2004 02:19
CODE
string c00000000;
string c00000001;
string c00000010;
string c00000100;
string c00001000;
string c00010000;
string c00100000;
string c01000000;

string xor(string a,string b) {

return llXorBase64Strings(a,b);
}

string xor64(string a,string b) {
a = llStringToBase64(a);
b = llStringToBase64(b);

return llXorBase64Strings(a,b);
}

integer tpow(integer exp) {
return (integer)llPow(2,exp);
}

string base64 (string a) {
return llStringToBase64(a);
}

generate_bases() {
c00000000 = xor64(" "," ");
c00000001 = xor64("A","@");
c00000010 = xor64("B","@");
c00000100 = xor64("D","@");
c00001000 = xor64("H","@");
c00010000 = xor64("P","@");
c00100000 = base64(" ");
c01000000 = base64("@");
}

string Int2Base64(integer num) {
// Converts a Small (0-127) Integer to Base64 Byte
// Written by Adam Zaius

if(num > tpow(7)) {
llSay(0,"ERROR: 7 bit numbers only.");
return "";
}

string byte = c00000000;
if(num >= tpow(6)) {
num -= tpow(6);
byte = xor(byte,c01000000);
}
if(num >= tpow(5)) {
num -= tpow(5);
byte = xor(byte,c00100000);
}
if(num >= tpow(4)) {
num -= tpow(4);
byte = xor(byte,c00010000);
}
if(num >= tpow(3)) {
num -= tpow(3);
byte = xor(byte,c00001000);
}
if(num >= tpow(2)) {
num -= tpow(2);
byte = xor(byte,c00000100);
}
if(num >= tpow(1)) {
num -= tpow(1);
byte = xor(byte,c00000010);
}
if(num >= tpow(0)) {
num -= tpow(0);
byte = xor(byte,c00000001);
}
return byte;
}


default
{
state_entry()
{
generate_bases();
}

touch_start(integer total_number)
{
string test = llBase64ToString(Int2Base64(87)); // The letter 'W'
llSay(0,test);
}
}


Now, it's limited at the moment, to 7bit integers, until we can XOR with the 8th bit. Once that's done, I'll upgrade it for all integer values (ie: 0 to 2^30 + signature)


Enjoy,
-Adam
_____________________
Co-Founder / Lead Developer
GigasSecondServer
Francis Chung
This sentence no verb.
Join date: 22 Sep 2003
Posts: 918
08-14-2004 19:50
Hi Adam,

We talked about this in-world, but I don't quite yet understand where the 7 bit limitation comes from. You only worry about the 7-bit limitation when you're going back from base64 to a string?

You're converting an int to a base64 representation, which can accurately represent any bitstream of arbitrary length?

For what it's worth, I have a base16tobase64 conversion function in my encryption library in the script forums.

/15/c2/13580/1.html

I use this function to convert MD5 sums into base64 "binary". You could trivially use this as a helper function to convert integer to binary without the 7-bit limitation, by first converting to a base16 string.

So, for instance

int x = 1234;

becomes

string y = "4D2";

And then calling my function, base16tobase64(y).
_____________________
--
~If you lived here, you would be home by now~
Adam Zaius
Deus
Join date: 9 Jan 2004
Posts: 1,483
08-15-2004 04:05
*removed for testing theory*

-Adam
_____________________
Co-Founder / Lead Developer
GigasSecondServer
Tiger Crossing
The Prim Maker
Join date: 18 Aug 2003
Posts: 1,560
08-15-2004 21:36
I'd just like to see a llListToBase64 and llBase64ToList... Something than can encode ANY data in a list into a emailable text string, and then reverse the process. Something built into the system so it doesn't lag sims when running.
_____________________
~ Tiger Crossing
~ (Nonsanity)
Adam Zaius
Deus
Join date: 9 Jan 2004
Posts: 1,483
08-16-2004 00:14
Alright, I've finished the testing I was going to do. My system is one bit-per-byte more efficient than yours (if I can get the 8th bit, it will be two bits); Here's why:

A Base64 string stores 6 bits per byte, to store one of 64 possible characters. (The unused two bits are zero-ed out). Now, I mis-labelled this, it should have been Integer2Base128; By using XOR'ing of certain Base64 sequences, I can get 128 possible values at the moment, rather than 64. If I convert back to an ordinary string sequence, any of the special characters generated are 'nulled out' with '?'s. So: I keep it as a base64 string, that uses one of the two normally zero-ed out bits. I'm working on getting the second running.

-Adam
_____________________
Co-Founder / Lead Developer
GigasSecondServer
Francis Chung
This sentence no verb.
Join date: 22 Sep 2003
Posts: 918
08-16-2004 17:36
Adam,

I haven't taken the time to fully understand your code, can you explain some parts?

It seems that you're converting 7-bit integers into characters.

The only useful function I can deduce from this is to save script memory, to pack 7-bit values as tightly as possible. Is that what you're doing with it? You can't export 7-bit values directly through email/xmlrpc/chat/IM, as far as I know, due to unprintable characters.

But, since you're converting to base 128, not base 64, don't you need a conversion function to go back from base 128 to integer?
_____________________
--
~If you lived here, you would be home by now~
Adam Zaius
Deus
Join date: 9 Jan 2004
Posts: 1,483
08-17-2004 01:10
Gack, again this was a very early in the morning bad explanation. I'll need to repost the code again to get the right number of bits stored.

It's mainly for packing yes, I'm still testing. The Base64 function there is only the first half. As I said, I'll need to post the other half before it becomes that smidgen more usefull. :)

-Adam
_____________________
Co-Founder / Lead Developer
GigasSecondServer
Adam Zaius
Deus
Join date: 9 Jan 2004
Posts: 1,483
08-17-2004 01:12
Your function btw, gave me an idea for generating the c10000000 that's been plaguing me.

-Adam
_____________________
Co-Founder / Lead Developer
GigasSecondServer