fork download
  1. using System;
  2.  
  3. public class Test
  4. {
  5. public static void Main()
  6. {
  7. string stringNumber = "-42";
  8. int number = StringToInteger(stringNumber);
  9.  
  10. System.Console.WriteLine(number);
  11. }
  12.  
  13. public static int StringToInteger(string value)
  14. {
  15. bool negative = false;
  16. int i = 0;
  17.  
  18. if (value[0] == '-')
  19. {
  20. negative = true;
  21. ++i;
  22. }
  23.  
  24. int number = 0;
  25. for (; i < value.Length; ++i)
  26. {
  27. var character = value[i];
  28. number = (number << 1) + (number << 3) + (character - '0');
  29. }
  30.  
  31. if (negative)
  32. number = -number;
  33. return number;
  34. }
  35. }
Success #stdin #stdout 0.02s 16044KB
stdin
Standard input is empty
stdout
-42