Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

How to remove excess zeros for better data transmission

Vilkacis Mason
Registered User
Join date: 30 Aug 2005
Posts: 49
04-17-2006 17:16
So my script prints a bunch of parameters relating to size, rotation, position, etc. A bunch of vectors mainly, with a few floats thrown in.

It looks like this:

CODE
<0.329193, -0.089701, 0.173800>, <-0.435410, 0.679684, 0.557196, 0.194884>, <0.050000, 0.050000, 0.050000>, 5748decc-f629-461c-9a36-a35a221fe21f, <0.100000, 0.100000, 0.000000>, <0.000000, 0.000000, 0.000000>, -1.570840, <1.000000, 1.000000, 1.000000>, 1.000000, 1, 0, 48, <0.199000, 0.850000, 0.000000>, 0.649000, <0.000000, 0.000000, 0.000000>, <0.000000, 1.000000, 0.000000>, <-0.500000, 0.000000, 0.000000>


Notice all the wasted zeros? This is fine if you are just recording the list to a variable, but I want to send these over E-Mail and to chat if needed, so those zeros at the end are wasting a lot of character length.

I need a way to cut <0.000000, 1.000000, 0.000000> down to <0.0, 1.0, 0.0>. I need to know what commands I should be using and maybe some suggestions about how I should use them to try to get the script to realize what is a wasted zero and what is a key.
Nepenthes Ixchel
Broadly Offended.
Join date: 6 Dec 2005
Posts: 696
04-17-2006 18:19
How about
CODE

string s=(string)number;
if (s contains ".")
{
while (last character of s=="0")
{
remove last character of s;
}
}
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
04-18-2006 01:26
CODE

string hexc="0123456789ABCDEF";

string Float2Hex(float a)
{// Copyright Strife Onizuka, 2006, LGPL, http://www.gnu.org/copyleft/lesser.html
if(a != 0)
{
float b = llFabs(a);
string f = "";
integer c = llFloor(llLog(b) / 0.69314718055994530941723212145818);//floor(log2(b))
integer d = (integer)((b / llPow(2.0,c)) * 0x1000000);//shift up into integer range
c -= 24;//the extra c used to make it an integer
while(!(d & 0xf))
{//strip extra zeros off before converting or they break "p"
d = d >> 4;
c+=4;
}
do
f = llGetSubString(hexc,15&d,15&d) + f;
while(d = d >> 4);
if(a < 0)
return "-0x" + f + "p"+(string)c;
return "0x" + f + "p"+(string)c;
}
return "0";//zero would screw up the log.
}

string vec(vector a)
{
if(a == ZERO_VECTOR) return "";
return "<"+flo(a.x)+","+flo(a.y)+","+flo(a.z)+">";
}

string flo(float a)
{
string b = (string)a;
while(llGetSubString(b,-1,-1) == "0")
b=llDeleteSubString(b,-1,-1);
if(llGetSubString(b,-1,-1) == ".")
return llDeleteSubString(b,-1,-1);
if(llGetSubString(b,(a<0),(a<0)+1)=="0.")
return llDeleteSubString(b,(a<0),(a<0));
return b;
}

string vecF2H(vector a)
{
if(a == ZERO_VECTOR) return "";
list b = ["<",flo(a.x),",",flo(a.y),",",flo(a.z),">"];
vector c = (vector)((string)b);
if(c.x != a.x)
b = llListReplaceList(b, [Float2Hex(a.x)], 1, 1);
if(c.y != a.y)
b = llListReplaceList(b, [Float2Hex(a.y)], 3, 3);
if(c.z != a.z)
b = llListReplaceList(b, [Float2Hex(a.z)], 5, 5);
return (string)b;
}

string rot(rotation a)
{
if(a == ZERO_ROTATION) return "";
list b = ["<",flo(a.x),",",flo(a.y),",",flo(a.z),",",flo(a.s),">"];
rotation c = (rotation)((string)b);
if(c.x != a.x)
b = llListReplaceList(b, [Float2Hex(a.x)], 1, 1);
if(c.y != a.y)
b = llListReplaceList(b, [Float2Hex(a.y)], 3, 3);
if(c.z != a.z)
b = llListReplaceList(b, [Float2Hex(a.z)], 5, 5);
if(c.s != a.s)
b = llListReplaceList(b, [Float2Hex(a.s)], 7, 7);
return (string)b;
}

_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river.
- Cyril Connolly

Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence.
- James Nachtwey