Jump to content

Invalid Typecast to Integer


Nuclear Slingshot
 Share

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

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

Recommended Posts

Yes , it s the expected behaviour 

 

It looks be the same comportement than the C function atoi : 

i quote http://www.cplusplus.com/reference/cstdlib/atoi/

 

1) The function first discards as many whitespace characters (as in isspace) as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many base-10 digits as possible, and interprets them as a numerical value.2) The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.3) If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed and zero is returned.

 

With a test in world , we get

integer x  =  (integer)"   123" =>  x value is 123 as in C with atoi function (1st point of the quote )

integer y  =  (integer)"123hello" =>  y value is 123 as in C with atoi function (2nd point of the quote )

integer z  =  (integer)"hello" =>  z value is 0 as in C with atoi function (3rd point of the quote )

 

 

Note : strangely it looks closer to the C atoi function than the Int32.TryParse C# function .

 
#include <stdio.h>      /* printf, fgets */#include <stdlib.h>     /* atoi */ int main (){  int x = atoi ("  123");  int y = atoi ("123hello");  int z = atoi ("hello");  printf ("%d %d %d.\n",x,y,z);  return 0;}
Output :
123 123 0
 
using System; class Program{    static void Main()    {        int x; int y ; int z;        Int32.TryParse("  123", out x);        Int32.TryParse("123hello", out y);        Int32.TryParse("hello", out z);         Console.WriteLine("{0} {1} {2}", x, y, z);  // Converted to -3            }}
Output : 123 0 0

 

Probably for historic reasons  because the lsl VM was done in C .

 

Link to comment
Share on other sites

Nevertheless an exception system  in LSL will be great !  ( as too a collection system, a customized event system, a "library" system, a reference system, nested types ... )

But i doubt we ll get one soon  . I even doubt we ll get one , in the future . ( because of the compatibilty with old outdated user contents)

It s hopeless and very depressing

 

Link to comment
Share on other sites

It will give wrong negatives ( negative tests not negative numbers) :

 

string data ="+3"; // worths 3 who is valid and the cast returns 3if ( (string) ( (integer) data) == data)llOwnerSay("'" + data + "' contains a valid integer");elsellOwnerSay("'" + data + "' doesn t contain a valid integer");

 Output : '+3' doesn t contain a valid integer

 

You will need to add an additional test :

if ( ( (string) ( (integer) data) == data) || ( "+" + (string) ( (integer) data) == data) )

 

And probably other tests if your input can be in hexadecimal form :

string data ="0x0045AB12"; // worths 4565778 who is valid and the cast returns 4565778

 Output : '0x0045AB12' doesn t contain a valid integer 

 

Third problem : you will need to trim left-zeros

string data ="03"; // worths 3 who is valid and the cast returns 3

  Output : '03' doesn t contain a valid integer

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 4057 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...