View unanswered posts | View active topics It is currently Sun Jul 22, 2018 4:53 am

 Page 1 of 1 [ 12 posts ]
 Print view Previous topic | Next topic
How to string to int ?
Author Message
Rookie

Joined: Tue Aug 14, 2012 8:40 pm
Posts: 12
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
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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)

Sat Oct 20, 2012 12:55 pm
Novice

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 76
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?

Last edited by amcerbu on Sun Oct 21, 2012 11:42 pm, edited 1 time in total.

Sun Oct 21, 2012 11:08 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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");

Sun Oct 21, 2012 11:29 pm

Joined: Thu May 24, 2012 12:15 pm
Posts: 722
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.

_________________
Check out our Blog! And our Facebook page!
Need help? Take a look at our updated help documentation and the ROBOTC Forums.

Last edited by JohnWatson on Thu Nov 01, 2012 12:44 pm, edited 1 time in total.

Mon Oct 22, 2012 10:09 am
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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

Joined: Thu May 24, 2012 12:15 pm
Posts: 722
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 updated help documentation and the ROBOTC Forums.

Mon Oct 22, 2012 12:52 pm
Guru

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1523
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
Expert

Joined: Fri Nov 02, 2012 12:07 am
Posts: 163
Location: California, USA
Re: How to string to int ?
 Code:string a = "fjhfjksfjkshf"int b = (string) a

_________________
string Robored = "Awesome"
~~Neil Balch~~

Mon Nov 12, 2012 12:57 am
Expert

Joined: Fri Nov 02, 2012 12:07 am
Posts: 163
Location: California, USA
Re: How to string to int ?
Why the fancy function????

_________________
string Robored = "Awesome"
~~Neil Balch~~

Wed Nov 21, 2012 12:47 am

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3654
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_conversion

You're basically forcing a (temporary) conversion between one type of variable to another.

= Xander

_________________
| Professional Conduit of Reasonableness
| (Title bestowed upon on the 8th day of November, 2013)
| My Blog: I'd Rather Be Building Robots
| ROBOTC 3rd Party Driver Suite: [Project Page]

Wed Nov 21, 2012 12:49 am
Expert

Joined: Fri Nov 02, 2012 12:07 am
Posts: 163
Location: California, USA
Re: How to string to int ?
ThANKS!

_________________
string Robored = "Awesome"
~~Neil Balch~~

Thu Nov 22, 2012 12:36 am
Display posts from previous:  Sort by
 Page 1 of 1 [ 12 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.