Java tutorial
//package com.java2s; public class Main { public static int parseInt(String string) throws Throwable { return parseInt(string, 10); } public static int parseInt(String string, int radix) throws Throwable { if (radix >= 2 && radix <= 36) { if (string == null) { throw invalidInt(string); } else { int length = string.length(); int i = 0; if (length == 0) { throw invalidInt(string); } else { boolean negative = string.charAt(i) == 45; if (negative) { ++i; if (i == length) { throw invalidInt(string); } } return parseInt(string, i, radix, negative); } } } else { throw new Throwable("Invalid radix: " + radix); } } private static int parseInt(String string, int offset, int radix, boolean negative) throws Throwable { int max = -2147483648 / radix; int result = 0; int next; for (int length = string.length(); offset < length; result = next) { int digit = digit(string.charAt(offset++), radix); if (digit == -1) { throw invalidInt(string); } if (max > result) { throw invalidInt(string); } next = result * radix - digit; if (next > result) { throw invalidInt(string); } } if (!negative) { result = -result; if (result < 0) { throw invalidInt(string); } } return result; } private static Throwable invalidInt(String s) throws Throwable { throw new Throwable("Invalid int: \"" + s + "\""); } private static int digit(int codePoint, int radix) { if (radix >= 2 && radix <= 36) { int result = -1; if (48 <= codePoint && codePoint <= 57) { result = codePoint - 48; } else if (97 <= codePoint && codePoint <= 122) { result = 10 + (codePoint - 97); } else if (65 <= codePoint && codePoint <= 90) { result = 10 + (codePoint - 65); } return result < radix ? result : -1; } else { return -1; } } }