I needed to convert strings to integers, but it seems the built in functions for this are not available in 4.2, at least I could not seem to get them to work. Silly me if I just missed something. Anyway, assuming that they are missing, here is a bit of code that worked for me:
public int Str2Int( string input )
{
int ret = 0; // built the result here
while (input.Length > 0) // stop when all chars have been processed
{
char c = input[0]; // get the first char
if (c < '0') break; // stop if a non-number is found
if (c > '9') break;
int n = (int)c - 48; // ascii value to a number, IE '1' = 49
ret = n + 10 * ret; // accumulate the result
input = input.Substring(1); // remove the first char
}
return ret; // return the result to caller
}
If someone else has a better way to go this, I would love to hear about it.
string to integer
Started by Robert L., Aug 11 2011 01:37 AM
2 replies to this topic
#1
Posted 11 August 2011 - 01:37 AM
#2
Posted 11 August 2011 - 04:11 AM
You mean that int.Parse(string) is not working in the 4.2 release?
Biggest fault of Netduino? It runs by electricity.
#3
Posted 11 August 2011 - 09:54 AM
You mean that int.Parse(string) is not working in the 4.2 release?
yes, at least I (in my befuddled state) was not able to get it working. It seemed to be missing as was the rest of System.Convert
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users