View unanswered posts | View active topics It is currently Wed Jul 23, 2014 5:46 pm






Reply to topic  [ 12 posts ] 
How to string to int ? 
Author Message
Rookie

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

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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
Profile
Novice

Joined: Sun Oct 21, 2012 10:01 pm
Posts: 76
Post 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
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 556
Post 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: 1234
Test 2: 0
Test 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: 1234
Test 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 8). 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 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
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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 8). 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
Profile
Site Admin
Site Admin

Joined: Thu May 24, 2012 12:15 pm
Posts: 556
Post 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
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post 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
Profile
Expert
User avatar

Joined: Fri Nov 02, 2012 12:07 am
Posts: 164
Location: California, USA
Post 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
Profile WWW
Expert
User avatar

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

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


Wed Nov 21, 2012 12:47 am
Profile WWW
Moderator
Moderator
User avatar

Joined: Wed Mar 05, 2008 8:14 am
Posts: 3162
Location: Rotterdam, The Netherlands
Post 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
Profile WWW
Expert
User avatar

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

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


Thu Nov 22, 2012 12:36 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 12 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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

Search for:
Jump to:  



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