add simple string to integer conversion function

Just a basic one in C4Script for now.
console-destruction
Maikel de Vries 2016-09-18 11:30:25 +02:00
parent fbb8ad5d87
commit 823b13d764
1 changed files with 16 additions and 0 deletions

View File

@ -50,3 +50,19 @@ global func CharIsUpperCase(int char)
{
return Inside(char, 65, 90);
}
// Converts a string to an integer if it consists of digits only.
global func StringToInteger(string str)
{
var integer = 0;
var power = 0;
for (var index = GetLength(str) - 1; index >= 0; index--)
{
var char = GetChar(str, index);
if (!CharIsDigit(char))
return nil;
integer += (char - 48) * 10**power;
power++;
}
return integer;
}