@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:

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:

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 amPosts: 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

JohnWatson

Site Admin

Joined: Thu May 24, 2012 12:15 pmPosts: 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?

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.

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