Jump to content

Help ! Turn a string into an integer ?


rasterscan
 Share

You are about to reply to a thread that has been inactive for 1958 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hi ! How do I turn a string into an integer ? The string will be a number, taken from the prims description.

 

This works :

 string chan = llGetObjectDesc(); // CHANNEL NUMBER WILL BE IN DESC.

 

the string chan will be whatever the object description is. It will be a number, any number. Um. How do I change it from a string into a number lsl can work with ?

Thanks for any help

 

 

Link to comment
Share on other sites

If the object description string is "123abc" then the integer will be 123.

However, if the object description string is "abc123" then the integer will be 0.

If I remember correctly.

Also, remember that storing channels in the description of an object may pose a severe security risk, because anyone in SL can read that description.

Edited by Arduenn Schwartzman
  • Like 1
  • Thanks 1
Link to comment
Share on other sites

  • 2 weeks later...

you can have a integer and a string in the prim's description, just seperate the two with a marker like a comma and just do llCSV2List

ie. prim description is abc,123
then do list desclist = llCSV2List(llGetObjectDesc());
string somestring = llList2String(desclist,0); // returns abc
integer channel = llList2Integer(desclist,1); // returns 123 as a integer

Edited by VenKellie
Link to comment
Share on other sites

Converting a string to a integer is called typecasting. I.e converting one data type to another.

Simple cheat sheet : 

 

---Converting a string to a integer---

integer myChannel = (integer)llGetObjectDescription.....

integer myChannel = (integer)"12345";

 

---Converting a list entry to a integer---

integer myChannel = llList2Integer(myList , listIndex);

 

---Other types of conversion---

(string) : Convert a type to a string, so....  llOwnerSay((string)myChannel);

(vector) : Convert a type to a vector, so.... vector myDirection = (vector)"<1.0,1.0,1.0>";

(rotation) : Convert a type to a rotation, so.... rotation myTurn = (rotation)"<1.0,1.0,1.0,1.0>";

(float) : Convert a type to a float, so.... float myDecimal = (float)myChannel.....so if your channel is 456, it will become 456.0 after conversion

(list) : Convert a type to a list, so.... list myList = (list)"Entry 0";

llList2String : Convert a list entry to a string, so.... myString=llList2String(myList,listIndex);

llList2Vector : Convert a list entry to a vector, so....myDirection=llList2Vector(myList,listIndex);

llList2Rotation : Convert a list entry to a rotation, so....myTurn=llList2Rotation(myList,listIndex);

llList2Float : Convert a list entry to a float, so....myDecimal=llList2Float(myList,listIndex);

 

---Other relevant snippets you might be interested in---

integer randomChannel = 1+(integer)llFrand(999);

integer randomChannelFromUsersKey = (integer)("0x"+llGetSubString((string)llGetOwner(),-8,-1));

integer simplePositiveChannelFromServerTime = llGetUnixTime()/10;

integer simpleNegativeChannelFromServerTime = -llGetUnixTime()/10;

 

Edited by chibiusa Ling
  • Like 2
Link to comment
Share on other sites

But remember that the various llList2* functions do not necessarily yield the value that you may be expecting unless the variables in the list match the same type you are extracting. So, for example:

vector vMyVector = llList2Vector( [ <1,2,3>, <4,5,6> ] , 0 );

will yield <1,2,3>, but

vector vMyVector = llList2Vector( [ "<1,2,3>", "<4,5,6>" ] , 0 );

will yield ZERO_VECTOR because the values in the list were stored as strings, not as vectors.  Unless you are sure that variables in a list are stored as the type you are hoping to extract, the safe way to write the conversion is

vector vMyVector = (vector) llList2String ( [ <1,2,3>, <4,5,6> ] , 0 );

Clearly, that's not necessary in the specific case I am using as an example, because I can see how I filled the list in the first place.  When you receive a list from somewhere else, however, you cannot always be sure whether it is loaded with vectors, rotations, floats, or whatever, so it's wise to assume that it's not the type you want. Typecasting of anything to a string always works, so you can always follow it up with explicit typecasting to whatever type you need.

  • Like 3
Link to comment
Share on other sites

On 11/16/2018 at 2:14 AM, Phate Shepherd said:

llList2Integer will automatically perform the string to integer conversion.

Last time I tried, it didn't. And neither did llParseString2List. But you are right. I just tried again. Apparently, LL has changed this somewhere within the past few years. Thanks for pointing that out.

Edited by Arduenn Schwartzman
Link to comment
Share on other sites

1 hour ago, Arduenn Schwartzman said:
On 11/15/2018 at 7:14 PM, Phate Shepherd said:

llList2Integer will automatically perform the string to integer conversion.

Last time I tried, it didn't. And neither did llParseString2List. But you are right. I just tried again. Apparently, LL has changed this somewhere within the past few years. Thanks for pointing that out. 

Be careful when relying on llList2Integer() to convert a bitwise string to integer as it won't work in LSO compiled scripts.

[02:33:03] llList2Integer([desc]) Mono: '0x10' = 16
[02:33:03] llList2Integer([desc]) LSO: '0x10' = 0

Link to comment
Share on other sites

14 hours ago, Rolig Loon said:

vector vMyVector = llList2Vector( [ "<1,2,3>", "<4,5,6>" ] , 0 );

will yield ZERO_VECTOR because the values in the list were stored as strings, not as vectors.  Unless you are sure that variables in a list are stored as the type you are hoping to extract, the safe way to write the conversion is

vector vMyVector = (vector) llList2String ( [ <1,2,3>, <4,5,6> ] , 0 );

this can drive people new to scripting bananas sometimes. Specially when reading stuff in from a notecard. Vectors, rotations, keys

dataserver(key query_id, string data) { mylist += [data]; }

 

  • Like 1
Link to comment
Share on other sites

When I'm reading lists, unless I'm absolutely certain about what type the data is -- I've read it from llGetObjectDetails, for instance, or llDetected*, or hard-coded the list myself -- I always use llList2String and cast that into the data type I want rather than relying on LSL to do it for me.   

It's fractionally more effort but it saves hours of frustration when things don't work as expected and I'm trying to find out why (llList2Key seems particularly treacherous, at least for me).

  • Like 2
Link to comment
Share on other sites

Here's a couple examples of how I typically do this....

First, there are cases where you have no obvious delimiters or anything in the field to help you.  In that case, you'll need some sort of routine to seek out some sort of index on which characters to take, and which to leave alone.

 

~llSubStringIndex(source string, string to match) can help you find a starting point if there is some kind of text pattern in there to look for.  From there you can use llGetSubString() to pull it out.

 

Of course there's almost always more than one way and this might not be the best, but it works for me and I typically take this approach below.

The trick for me is to use some sort of delimiter character when possible.  I can be some character, or spaces, or even sets/groups of characters.  In this case I use a colon.

1.  If possible, keep your information separated with some sort of delimiter.

2.  Parse the field of text you want to work with into a list.

In this case I'm getting info form the script name instead of the object name or description, but happen to have it in front of me right now so it's easy to cut and paste...

 

I'll be grabbing info from a script named something like this:

+:-:CMG:0.1:100:10:0:0:200:1:MiniGuns:GunCtrl v1.3

	//Parse the script name into a list so it's easier to work with.
        list tank = llParseString2List(llGetScriptName(),[":"], [""]);
        
        //Grab the script number and Display Name. 
        string str = llStringTrim(llList2String(tank, 9), STRING_TRIM);
        if ((string)((integer)str) == str) {
            bulletCounter = [(integer)str];
            str = llStringTrim(llList2String(tank, 10), STRING_TRIM);
            bulletCounter += [str,-1]; 
            llOwnerSay("Script Number: "+(string)llList2Integer(bulletCounter,0));
            llOwnerSay("Gun(s) Display Name: "+llList2String(bulletCounter, 1));
        } else {
            bulletCounter = [1,"Bullets",-1];
            llOwnerSay("Script Name Error: Please make sure this script is named properly.  Each GunCtrl Script should have a unique Number and Name.  "+
                       "Trying a Default of: "+llList2CSV(bulletCounter));
        }

 

 

Edited by Credo Rolland
Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 1958 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...