C# code as below:
public static long ConvertStringToLong(string str)
{
str = str.Trim();
long m = 0;
int l = 1;
int startIndex = 0;
if (str[0] == '-')
{
l = -1;
startIndex++;
}
for (int i = startIndex; i < str.Length; i++)
{
m *= 10;
m += str[i];
}
return m * l;
}