java基础之Integer.parseInt(String s ,int radix)方法使用解释

来源:互联网 发布:风云同步助手软件 编辑:程序博客网 时间:2024/06/05 03:38
方法parseInt(String s,int radix)的目的是输出一个十进制数,这个数字是“String s”但是我们要知道他是多少进制的,而方法中“int radix”参数正是来表达这个信息的。 

比如:parseInt(1010,2) 
意思就是:输出2进制数1010在十进制下的数. 
更直白地说:parseInt(String s,int radix)就是求“int radix”进制数“String s”的十进制数是多少。 

----------------------- 

我们平时用到Integer.parseInt("123");其实默认是调用了int i =Integer.parseInt("123",10);  
其中10代表的默认是10进制的,转换的过程可以看成:  
                          
            i=  1*10*10+2*10+3  
若是  
               int i = Integer.parseInt("123",16);  
即可以看成:  
               i  = 1*16*16+2*16+3 = 291 
  
根据:Character.MIN_RADIX=2和Character.MAX_RADIX=36 则,parseInt(String s, int radix)参数中  
radix的范围是在2~36之间,超出范围会抛异常。其中s的长度也不能超出7,否则也会抛异常。  


源码如下: 

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