关于Integer的parseInt(String s, int radix)方法的使用

本文详细解析了Java中Integer.parseInt方法的工作原理及其使用方式,包括不同进制数的转换过程及注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近看了一些别人写的java程序,其中就用到Integer.parseInt("23f34d",16);这个方法,当时很不解。

在网上搜了一下,才明白原来是这样的。

 

首先可以看一下jdk中 java.lang.Integer中的源码如下:

Java代码   收藏代码
  1. public static int parseInt(String s) throws NumberFormatException   
  2. {  
  3.     return parseInt(s,10);  
  4.  }  
 
Java代码   收藏代码
  1.    public static int parseInt(String s, int radix)throws NumberFormatException  
  2.    {  
  3.        if (s == null) {  
  4.            throw new NumberFormatException("null");  
  5.        }  
  6.   
  7. if (radix < Character.MIN_RADIX) {          //Character.MIN_RADIX=2  
  8.     throw new NumberFormatException("radix " + radix +  
  9.                     " less than Character.MIN_RADIX");  
  10. }  
  11.   
  12. if (radix > Character.MAX_RADIX) {          //Character.MAN_RADIX=36  
  13.     throw new NumberFormatException("radix " + radix +  
  14.                     " greater than Character.MAX_RADIX");  
  15. }  
  16.   
  17. int result = 0;  
  18. boolean negative = false;  
  19. int i = 0, max = s.length();  
  20. int limit;  
  21. int multmin;  
  22. int digit;  
  23.   
  24. if (max > 0) {  
  25.     if (s.charAt(0) == '-') {  
  26.     negative = true;  
  27.     limit = Integer.MIN_VALUE;  
  28.     i++;  
  29.     } else {  
  30.     limit = -Integer.MAX_VALUE;  
  31.     }  
  32.     multmin = limit / radix;  
  33.     if (i < max) {  
  34.     digit = Character.digit(s.charAt(i++),radix);  
  35.     if (digit < 0) {  
  36.         throw NumberFormatException.forInputString(s);  
  37.     } else {  
  38.         result = -digit;  
  39.     }  
  40.     }  
  41.     while (i < max) {  
  42.     // Accumulating negatively avoids surprises near MAX_VALUE  
  43.     digit = Character.digit(s.charAt(i++),radix);  
  44.     if (digit < 0) {  
  45.         throw NumberFormatException.forInputString(s);  
  46.     }  
  47.     if (result < multmin) {  
  48.         throw NumberFormatException.forInputString(s);  
  49.     }  
  50.     result *= radix;  
  51.     if (result < limit + digit) {  
  52.         throw NumberFormatException.forInputString(s);  
  53.     }  
  54.     result -= digit;  
  55.     }  
  56. else {  
  57.     throw NumberFormatException.forInputString(s);  
  58. }  
  59. if (negative) {  
  60.     if (i > 1) {  
  61.     return result;  
  62.     } else {    /* Only got "-" */  
  63.     throw NumberFormatException.forInputString(s);  
  64.     }  
  65. else {  
  66.     return -result;  
  67. }  
  68.    }  

 

Java代码   收藏代码
  1. 我们平时用到Integer.parseInt("123");其实默认是调用了int i =Integer.parseInt("123"10);  
  2. 其中10代表的默认是10进制的,转换的过程可以看成:  
  3.                           
  4.             i=  1*10*10+2*10+3  
  5. 若是  
  6.                int i = Integer.parseInt("123"16);  
  7. 即可以看成:  
  8.                i  = 1*16*16+2*16+3  
  9.   
  10. 根据:Character.MIN_RADIX=2和Character.MAX_RADIX=36 则,parseInt(String s, int radix)参数中  
  11. radix的范围是在2~36之间,超出范围会抛异常。其中s的长度也不能超出7,否则也会抛异常。  
分享到:   
参考知识库
Node.js知识库 5  关注 | 218  收录
算法与数据结构知识库 1051  关注 | 2080  收录
Git知识库 779  关注 | 346  收录
jQuery知识库 740  关注 | 239  收录
评论
1 楼  huxiulei 2013-01-09  
radix的范围是在2~36之间,超出范围会抛异常。其中s的长度也不能超出7,否则也会抛异常。 
'


其中s的长度也不能超出7是什么意思? 
String bindString = "0000000011";
int bindInt = Integer.parseInt(bindString, 28);
System.out.println(bindString.length());
System.out.println(bindInt);
bindString 长度是十位   运行没有报错       这种算么? 求指点
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值