Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert a string to an integer?
Apjjm Offline
Is easy to say

Posts: 496
Threads: 18
Joined: Apr 2011
Reputation: 52
#4
RE: How to convert a string to an integer?

I wrote a function some time back that parses out all the digits from a string and converts them into one integer. E.g: "1a2b3c" would become 123.

int getDigit(uint8 digit) {
    int d = digit-48; //48 is ASCII code for 0
    return ((d >= 0)&&(d<=9)) ? d : -1;
}
int parseStringINT(string str) {
    int output = 0;
    for(int i=0; i<str.length(); i++)
     {
      int digit = getDigit(str[i]);
      if(digit > -1) output = 10*output+digit;  
     }
    return output;
}
(This post was last modified: 06-11-2011, 03:00 PM by Apjjm.)
06-11-2011, 02:57 PM
Find


Messages In This Thread
How to convert a string to an integer? - by cook - 06-11-2011, 05:26 AM
RE: How to convert a string to an integer? - by Apjjm - 06-11-2011, 02:57 PM



Users browsing this thread: 1 Guest(s)