修复16进制解析错误的问题

This commit is contained in:
Looly 2023-09-12 17:54:30 +08:00
parent 880bd40870
commit ead980e246
2 changed files with 8 additions and 5 deletions

View File

@ -106,16 +106,16 @@ public class NumberParser {
return 0;
}
if (StrUtil.containsIgnoreCase(numberStr, "E")) {
// 科学计数法忽略支持科学计数法一般用于表示非常小和非常大的数字这类数字转换为int后精度丢失没有意义
throw new NumberFormatException(StrUtil.format("Unsupported int format: [{}]", numberStr));
}
if (StrUtil.startWithIgnoreCase(numberStr, "0x")) {
// 0x04表示16进制数
return Integer.parseInt(numberStr.substring(2), 16);
}
if (StrUtil.containsIgnoreCase(numberStr, "E")) {
// 科学计数法忽略支持科学计数法一般用于表示非常小和非常大的数字这类数字转换为int后精度丢失没有意义
throw new NumberFormatException(StrUtil.format("Unsupported int format: [{}]", numberStr));
}
try {
return Integer.parseInt(numberStr);
} catch (final NumberFormatException e) {

View File

@ -393,6 +393,9 @@ public class NumberUtilTest {
int number = NumberUtil.parseInt("0xFF");
Assertions.assertEquals(255, number);
number = NumberUtil.parseInt("0xFE");
Assertions.assertEquals(254, number);
// 0开头
number = NumberUtil.parseInt("010");
Assertions.assertEquals(10, number);