Java tutorial
//package com.java2s; public class Main { public static long parseLong(String string) throws Throwable { return parseLong(string, 10); } public static long parseLong(String string, int radix) throws Throwable { if (radix >= 2 && radix <= 36) { if (string == null) { throw new Throwable("Invalid long: \"" + string + "\""); } else { int length = string.length(); int i = 0; if (length == 0) { throw new Throwable("Invalid long: \"" + string + "\""); } else { boolean negative = string.charAt(i) == 45; if (negative) { ++i; if (i == length) { throw new Throwable("Invalid long: \"" + string + "\""); } } return parseLong(string, i, radix, negative); } } } else { throw new Throwable("Invalid radix: " + radix); } } private static long parseLong(String string, int offset, int radix, boolean negative) throws Throwable { long max = -9223372036854775808L / (long) radix; long result = 0L; long next; for (long length = (long) string.length(); (long) offset < length; result = next) { int digit = digit(string.charAt(offset++), radix); if (digit == -1) { throw new Throwable("Invalid long: \"" + string + "\""); } if (max > result) { throw new Throwable("Invalid long: \"" + string + "\""); } next = result * (long) radix - (long) digit; if (next > result) { throw new Throwable("Invalid long: \"" + string + "\""); } } if (!negative) { result = -result; if (result < 0L) { throw new Throwable("Invalid long: \"" + string + "\""); } } return result; } 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; } } }