| Author |
Message |
|
kevin.info
Rookie
Joined: Tue Aug 14, 2012 8:40 pm Posts: 10
|
 How to string to int ?
Hi everyone Can anyone let me know how to convert a string to int.
Thx kevin
|
| Sat Oct 20, 2012 9:15 am |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 Re: How to string to int ?
There is a built-in RobotC function called atoi().
|
| Sat Oct 20, 2012 12:55 pm |
|
 |
|
amcerbu
Novice
Joined: Sun Oct 21, 2012 10:01 pm Posts: 76
|
 Re: How to string to int ?
So it would work like this? @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?
Last edited by amcerbu on Sun Oct 21, 2012 11:42 pm, edited 1 time in total.
|
| Sun Oct 21, 2012 11:08 pm |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 Re: How to string to int ?
atoi is exactly what it said "ascii to integer". If you have a floating point number, then there is atof.
|
| Sun Oct 21, 2012 11:29 pm |
|
 |
|
JohnWatson
Site Admin
Joined: Thu May 24, 2012 12:15 pm Posts: 380
|
 Re: How to string to 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: This code results in the output: 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: 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: Which will return: 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.
_________________Check out our Blog! And our Facebook page! Need help? Take a look at our Wiki and our Forums.I just met you, And this is crazy, But here's my code now, So fix it, maybe? ~ Carly Rae Jepsen parody
Last edited by JohnWatson on Thu Nov 01, 2012 12:44 pm, edited 1 time in total.
|
| Mon Oct 22, 2012 10:09 am |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 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.
|
| Mon Oct 22, 2012 12:30 pm |
|
 |
|
JohnWatson
Site Admin
Joined: Thu May 24, 2012 12:15 pm Posts: 380
|
 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?
_________________Check out our Blog! And our Facebook page! Need help? Take a look at our Wiki and our Forums.I just met you, And this is crazy, But here's my code now, So fix it, maybe? ~ Carly Rae Jepsen parody
|
| Mon Oct 22, 2012 12:52 pm |
|
 |
|
MHTS
Guru
Joined: Sun Nov 15, 2009 5:46 am Posts: 1023
|
 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.
|
| Mon Oct 22, 2012 1:07 pm |
|
 |
|
roboRed
Rookie
Joined: Fri Nov 02, 2012 12:07 am Posts: 31
|
 Re: How to string to int ?
_________________ string Robored = "Awesome"
|
| Mon Nov 12, 2012 12:57 am |
|
 |
|
roboRed
Rookie
Joined: Fri Nov 02, 2012 12:07 am Posts: 31
|
 Re: How to string to int ?
Why the fancy function???? 
_________________ string Robored = "Awesome"
|
| Wed Nov 21, 2012 12:47 am |
|
 |
|
mightor
Moderator
Joined: Wed Mar 05, 2008 8:14 am Posts: 2860 Location: Rotterdam, The Netherlands
|
 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
_________________| Some people, when confronted with a problem, think, "I know, I'll use threads," | and then two they hav erpoblesms. (@nedbat)| My Blog: I'd Rather Be Building Robots| ROBOTC 3rd Party Driver Suite: [ Project Page]
|
| Wed Nov 21, 2012 12:49 am |
|
 |
|
roboRed
Rookie
Joined: Fri Nov 02, 2012 12:07 am Posts: 31
|
 Re: How to string to int ?
ThANKS! 
_________________ string Robored = "Awesome"
|
| Thu Nov 22, 2012 12:36 am |
|
|