ROBOTC.net forumshttp://robotc.net/forums/ How to string to int ?http://robotc.net/forums/viewtopic.php?f=1&t=5006 Page 1 of 1

 Author: kevin.info [ Sat Oct 20, 2012 9:15 am ] Post subject: How to string to int ? Hi everyone Can anyone let me know how to convert a string to int. Thx kevin

Author:  MHTS [ Sat Oct 20, 2012 12:55 pm ]
Post subject:  Re: How to string to int ?

There is a built-in RobotC function called atoi().
 Code:intrinsic long  atoi(const string &sSrce)intrinsic long  atoi(char *pzSrce)

Author:  amcerbu [ Sun Oct 21, 2012 11:08 pm ]
Post subject:  Re: How to string to int ?

So it would work like this?
 Code:long Number = 0;Number = atoi("1000");

@MHTS: What value is returned by atoi() if the string argument sSrc or pzSrc doesn't strictly contain decimal digits? If there's a decimal place, is the number rounded down to an int?

Author:  MHTS [ Sun Oct 21, 2012 11:29 pm ]
Post subject:  Re: How to string to int ?

amcerbu wrote:
So it would work like this?
 Code:long Number = 0;Number = atoi("1000");

@MHTS: What value is returned by atoi() if the string arguments sSrc or pzSrc doesn't strictly contain decimal digits? If there's a decimal place, is the number rounded down to an int?

atoi is exactly what it said "ascii to integer". If you have a floating point number, then there is atof.
 Code:int IntegerNumber = 0;float FloatingPointNumber = 0.0;IntegerNumber = atoi("1000");FloatingPointNumber = atof("1000.0");

Author:  JohnWatson [ Mon Oct 22, 2012 10:09 am ]
Post subject:  Re: How to string to int ?

amcerbu wrote:
So it would work like this?
 Code:long Number = 0;Number = atoi("1000");

@MHTS: What value is returned by atoi() if the string argument sSrc or pzSrc doesn't strictly contain decimal digits? If there's a decimal place, is the number rounded down to an int?

If you attempt to use atoi on a non-integer string (meaning, on the string "Hello", for example), atoi will NOT convert the individual letters to their ASCII equivalents. It will, however, partially convert a string that starts with an integer and has non-integer characters mixed in. For example:

 Code:task main(){   string String1 = "1234.56789";   string String2 = "Hello";  string String3 = "123Hello45.6789";     int Test1 = atoi(String1);   int Test2 = atoi(String2);  int Test3 = atoi(String3);     writeDebugStreamLine("Test 1: %d", Test1);   writeDebugStreamLine("Test 2: %d", Test2);   writeDebugStreamLine("Test 3: %d", Test3);}

This code results in the output:

 Quote:Test 1: 1234Test 2: 0Test 3: 123

The reason for this is that atoi checks the first character for a 0-9 integer value and if found, converts it to an integer. When it comes across a non-integer character it stops converting the characters over.

In the case of Test 1, the decimal '.' is the non-integer value that stops atoi, so only the integers before it (reading left to right) are converted. Test 2 starts with a non-integer character so no characters are converted nor assigned to Test2. Test 3 stops converting when it reaches the 'H' character in "Hello".

Note that Test 1 can be fully converted with all of the values intact if using the atof (ASCII to float) function instead:

 Code:task main(){   string String1 = "1234.56789";    int Test4 = atoi(String1);   float Test5 = atof(String1);      writeDebugStreamLine("Test 4: %d", Test4);   writeDebugStreamLine("Test 5: %f", Test5); }

 Quote:Test 4: 1234Test 5: 1234.567871

Because the float data type is only reliable for up to ~7 decimal digits, ROBOTC is able to correctly convert the first four decimal digits (1, 2, 3, and 4), the decimal point, and the next four decimal digits (5, 6, 7, and . Anything beyond that may or may not calculate correctly, so please be aware of these limitations.

Last but certainly not least, you can also use atoi/atof with character arrays:
 Code:task main(){              char temp[] = "55.32";   float Test6 = atof(temp);   writeDebugStreamLine("Test 6: %f", Test6);}

Which will return:
 Quote:Test 6: 55.320000

Again, unless you specify the amount of decimal places to display in the writeDebugStreamLine, ROBOTC will display the highest resolution it can for the float (8 decimal digits).

We have a full listing of all of the Math commands on our Wiki that you may want to take a look at as well.

Author:  MHTS [ Mon Oct 22, 2012 12:30 pm ]
Post subject:  Re: How to string to int ?

 JohnWatson wrote:Because the float data type is only reliable for up to ~7 decimal digits, ROBOTC is able to correctly convert the first for decimal digits (1, 2, 3, and 4), the decimal point, and the next four decimal digits (5, 6, 7, and . Anything beyond that may or may not calculate correctly, so please be aware of these limitations.

That reminded me a question: does RobotC support "double"? I assume "float" in RobotC is 32-bit and "double" is 64-bit. In most scenarios float is fine, but in some case, we may need double.

 Author: JohnWatson [ Mon Oct 22, 2012 12:52 pm ] Post subject: Re: How to string to int ? At this point ROBOTC only supports 32 bit data types; while we do not have any plans to support 64 bit data types, we do appreciate the feedback and will take it into consideration for future development. Out of personal curiosity, what would you be using the double types for?

 Author: MHTS [ Mon Oct 22, 2012 1:07 pm ] Post subject: Re: How to string to int ? Like I said, in most scenarios, I only need float. But for scenarios where a floating point number is involved in a complex expression and it is multiplied with a big number. Precision may be necessary.

Author:  roboRed [ Mon Nov 12, 2012 12:57 am ]
Post subject:  Re: How to string to int ?

 Code:string a = "fjhfjksfjkshf"int b = (string) a

 Author: roboRed [ Wed Nov 21, 2012 12:47 am ] Post subject: Re: How to string to int ? Why the fancy function????

 Author: mightor [ Wed Nov 21, 2012 12:49 am ] Post subject: Re: How to string to int ? It's called a type cast, it's not a function. You can read about casting here: http://en.wikipedia.org/wiki/Type_conversionYou're basically forcing a (temporary) conversion between one type of variable to another.= Xander

 Author: roboRed [ Thu Nov 22, 2012 12:36 am ] Post subject: Re: How to string to int ? ThANKS!

 Page 1 of 1 All times are UTC - 5 hours [ DST ] Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Grouphttp://www.phpbb.com/