public static Long decode(String nm) { int radix = 10; int index = 0; boolean negative = false; Long result; if (nm.length() == 0) { throw new NumberFormatException("Zero length string"); } char firstChar = nm.charAt(0); // Handle sign, if present if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') { index++; } // Handle radix specifier, if present if (nm.startsWith(new String("0x"), index) || nm.startsWith(new String("0X"), index)) { index += 2; radix = 16; } else if (nm.startsWith(new String("#"), index)) { index++; radix = 16; } else if (nm.startsWith(new String("0"), index) && nm.length() > 1 + index) { index++; radix = 8; } if (nm.startsWith(new String("-"), index) || nm.startsWith(new String("+"), index)) { throw new NumberFormatException("Sign character in wrong position"); } try { result = Long.valueOf(nm.substring(index), radix); result = negative ? Long.valueOf(-result.longValue()) : result; } catch (NumberFormatException) { // If number is Long.MIN_VALUE, we'll end up here. The next line // handles this case, and causes any genuine format error to be // rethrown. String constant = negative ? new String("-" + nm.substring(index)) : nm.substring(index); result = Long.valueOf(constant, radix); } return(result); }
public static Long decode(String nm) { int radix = 10; int index = 0; boolean negative = false; Long result; if (nm.length() == 0) throw new NumberFormatException("Zero length string"); char firstChar = nm.charAt(0); // Handle sign, if present if (firstChar == '-') { negative = true; index++; } else if (firstChar == '+') index++; // Handle radix specifier, if present if (nm.startsWith(new String("0x"), index) || nm.startsWith(new String("0X"), index)) { index += 2; radix = 16; } else if (nm.startsWith(new String("#"), index)) { index ++; radix = 16; } else if (nm.startsWith(new String("0"), index) && nm.length() > 1 + index) { index ++; radix = 8; } if (nm.startsWith(new String("-"), index) || nm.startsWith(new String("+"), index)) throw new NumberFormatException("Sign character in wrong position"); try { result = Long.valueOf(nm.substring(index), radix); result = negative ? Long.valueOf(-result.longValue()) : result; } catch (NumberFormatException) { // If number is Long.MIN_VALUE, we'll end up here. The next line // handles this case, and causes any genuine format error to be // rethrown. String constant = negative ? new String("-" + nm.substring(index)) : nm.substring(index); result = Long.valueOf(constant, radix); } return result; }