What is the equivalent in C# of strtol function in C

Empty

3 Replies

CB Clay Burch Syncfusion Team May 19, 2002 06:18 AM UTC

You can use the static System.Int64.Parse method to convert a string to a signed 8-byte integer. In general, all the numeric types have this static Parse method. string s = "12345678"; long x = Int64.Parse(s); More generally, you can also use the System.Convert class to handle conversion among types.


MM Mukesh Motwani May 19, 2002 10:06 AM UTC

System.Convert.ToInt64 works fine if there are no spaces. If I have a string like "128 128" and if the number of white spaces is not known using the code below in C, we can easily find the numeric variables. /*C code*/ char string="128 128",*ptr; NO_COL=strtol(string,&ptr,0); NO_ROW=atoi(ptr); The string is scanned up to the first character inconsistent with the base. Leading "white-space" characters are ignored. If the value of ptr is not (char **)NULL, a pointer to the character terminating the scan is returned in the location pointed to by ptr. Is there an equivalent function in C# which will tells us the index in the string where the first character inconsistent with the base is returned using the different Formating Types provided by C#. Thanks Mukesh > You can use the static System.Int64.Parse method to convert a string to a signed 8-byte integer. In general, all the numeric types have this static Parse method. > > string s = "12345678"; > > long x = Int64.Parse(s); > > More generally, you can also use the System.Convert class to handle conversion among types. > >


CB Clay Burch Syncfusion Team May 19, 2002 07:11 PM UTC

If your whitespace is really spaces, then you could strip them out with code such as this. string s = "123 45678"; long x = Int64.Parse(s.Replace(" ", ""));

Loader.
Up arrow icon