1. 程式人生 > >java基礎之Integer.parseInt(String s ,int radix)方法使用解釋

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

方法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,否則也會拋異常。  


原始碼如下: 

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