Integer.decode("-011")
public static Integer decode(String nm) throws NumberFormatException
将 String 解码为 Integer。接受通过以下语法给出的十进制、十六进制和八进制数字:
DecodableString:
Signopt DecimalNumeral
Signopt 0x HexDigits
Signopt 0X HexDigits
Signopt # HexDigits
Signopt 0 OctalDigits
跟在(可选)负号和/或基数说明符(“0x”、“0X”、“#”或前导零)后面的字符序列是使用指示的基数(10、16 或 8)通过 Integer.parseInt 方法解析的。字符序列必须表示一个正值,否则会抛出 NumberFormatException。如果指定的 String 的第一个字符是减号,则对结果求反。String 中不允许出现空白字符。
public static Integer decode(String nm) throws NumberFormatException {
int radix = 10;
int index = 0;
boolean negative = false;
Integer result;
// Handle minus sign, if present
if (nm.startsWith("-")) {
negative = true;
index++;
}
// Handle radix specifier, if present
if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
index += 2;
radix = 16;
}
else if (nm.startsWith("#", index)) {
index ++;
radix = 16;
}
else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
index ++;
radix = 8;
}
if (nm.startsWith("-", index))
throw new NumberFormatException("Negative sign in wrong position");
try {
result = Integer.valueOf(nm.substring(index), radix);
result = negative ? Integer.valueOf(-result.intValue()) : result;
} catch (NumberFormatException e) {
// If number is Integer.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 ? "-" + nm.substring(index)
: nm.substring(index);
result = Integer.valueOf(constant, radix);
}
return result;
}
public static Integer decode(String nm) throws NumberFormatException
将 String 解码为 Integer。接受通过以下语法给出的十进制、十六进制和八进制数字:
DecodableString:
Signopt DecimalNumeral
Signopt 0x HexDigits
Signopt 0X HexDigits
Signopt # HexDigits
Signopt 0 OctalDigits
跟在(可选)负号和/或基数说明符(“0x”、“0X”、“#”或前导零)后面的字符序列是使用指示的基数(10、16 或 8)通过 Integer.parseInt 方法解析的。字符序列必须表示一个正值,否则会抛出 NumberFormatException。如果指定的 String 的第一个字符是减号,则对结果求反。String 中不允许出现空白字符。
public static Integer decode(String nm) throws NumberFormatException {
int radix = 10;
int index = 0;
boolean negative = false;
Integer result;
// Handle minus sign, if present
if (nm.startsWith("-")) {
negative = true;
index++;
}
// Handle radix specifier, if present
if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {
index += 2;
radix = 16;
}
else if (nm.startsWith("#", index)) {
index ++;
radix = 16;
}
else if (nm.startsWith("0", index) && nm.length() > 1 + index) {
index ++;
radix = 8;
}
if (nm.startsWith("-", index))
throw new NumberFormatException("Negative sign in wrong position");
try {
result = Integer.valueOf(nm.substring(index), radix);
result = negative ? Integer.valueOf(-result.intValue()) : result;
} catch (NumberFormatException e) {
// If number is Integer.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 ? "-" + nm.substring(index)
: nm.substring(index);
result = Integer.valueOf(constant, radix);
}
return result;
}