How to convert a string value to a number
Every programming language has the function of converting string to number.
But do you know how this is done?
Actually, it’s a very simple process.
For example to the “12” string
In a loop, first, get elements in string convert to ascii code, second, ascii code – 48 (because, code 48 represents 0 in ascii table), after then result, multiplied by 10 and assigned to variable.
For that case;
10 * variable + (49 - 48)
10 * 0 (first) + 1
1
And here is another example, write with C#
(Source: My Gist)
static int intParse(string value)
{
int newVal = 0;
foreach (char letter in value)
{
newVal = 10 * newVal + (letter - 48);
}
return newVal;
}