1231 * This sequence of characters must represent a positive value or
1232 * a {@link NumberFormatException} will be thrown. The result is
1233 * negated if first character of the specified {@code String} is
1234 * the minus sign. No whitespace characters are permitted in the
1235 * {@code String}.
1236 *
1237 * @param nm the {@code String} to decode.
1238 * @return a {@code Long} object holding the {@code long}
1239 * value represented by {@code nm}
1240 * @throws NumberFormatException if the {@code String} does not
1241 * contain a parsable {@code long}.
1242 * @see java.lang.Long#parseLong(String, int)
1243 * @since 1.2
1244 */
1245 public static Long decode(String nm) throws NumberFormatException {
1246 int radix = 10;
1247 int index = 0;
1248 boolean negative = false;
1249 Long result;
1250
1251 if (nm.length() == 0)
1252 throw new NumberFormatException("Zero length string");
1253 char firstChar = nm.charAt(0);
1254 // Handle sign, if present
1255 if (firstChar == '-') {
1256 negative = true;
1257 index++;
1258 } else if (firstChar == '+')
1259 index++;
1260
1261 // Handle radix specifier, if present
1262 if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
1263 index += 2;
1264 radix = 16;
1265 }
1266 else if (nm.startsWith("#", index)) {
1267 index ++;
1268 radix = 16;
1269 }
1270 else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
1271 index ++;
|
1231 * This sequence of characters must represent a positive value or
1232 * a {@link NumberFormatException} will be thrown. The result is
1233 * negated if first character of the specified {@code String} is
1234 * the minus sign. No whitespace characters are permitted in the
1235 * {@code String}.
1236 *
1237 * @param nm the {@code String} to decode.
1238 * @return a {@code Long} object holding the {@code long}
1239 * value represented by {@code nm}
1240 * @throws NumberFormatException if the {@code String} does not
1241 * contain a parsable {@code long}.
1242 * @see java.lang.Long#parseLong(String, int)
1243 * @since 1.2
1244 */
1245 public static Long decode(String nm) throws NumberFormatException {
1246 int radix = 10;
1247 int index = 0;
1248 boolean negative = false;
1249 Long result;
1250
1251 if (nm.isEmpty())
1252 throw new NumberFormatException("Zero length string");
1253 char firstChar = nm.charAt(0);
1254 // Handle sign, if present
1255 if (firstChar == '-') {
1256 negative = true;
1257 index++;
1258 } else if (firstChar == '+')
1259 index++;
1260
1261 // Handle radix specifier, if present
1262 if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
1263 index += 2;
1264 radix = 16;
1265 }
1266 else if (nm.startsWith("#", index)) {
1267 index ++;
1268 radix = 16;
1269 }
1270 else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
1271 index ++;
|